Ответы пользователя по тегу PHP
  • Не происходит удаления в таблице?

    Uman
    @Uman Автор вопроса
    PHP, YII2
    Все решил, надо было сделать проверку
    if ($this->taste_array) {
                foreach ($this->taste_array as $one) {
                    if (!in_array($one, $arr)) {
                        $model = ($model = HasProductTaste::find()
                            ->where(['product_id' => $this->id])
                            ->andWhere(['taste_id' => $one])
                            ->one()) ? $model : new HasProductTaste();
    
                        $model->product_id = $this->id;
                        $model->taste_id = $one;
                        $model->save();
                    }
                    if (isset($arr[$one])) {
                        unset($arr[$one]);
                    }
                }
            }
    Ответ написан
    Комментировать
  • Как связать Xdebug с PHPStorm?

    Привет, если работаешь в Ubuntu то сделай так
    sudo apt-get install php5-xdebug -y
    
    sudo nano /etc/php5/mods-available/xdebug.ini
    
    zend_extension=xdebug.so
    
    xdebug.default_enable=1
    xdebug.var_display_max_depth=6
    xdebug.remote_enable=1
    xdebug.remote_host=localhost
    xdebug.remote_port=9000
    xdebug.remote_handler=dbgp
    xdebug.remote_autostart=1
    xdebug.remote_log=/tmp/xdebug.log
    xdebug.idekey="PHPSTORM"
    xdebug.profiler_enable_trigger=1
    xdebug.profiler_enable=0
    xdebug.profiler_output_dir=/tmp/profiler
    xdebug.show_local_vars=1
    xdebug.overload_var_dump=1
    Ответ написан
  • Как получить данные с помощью рекурсии?

    В модели category сделал виртуальное свойство
    public $childs;

    в модели
    /**
         * Строим дерево категории
         *
         * @param $data
         * @param int $rootID
         * @return array
         */
        protected function buildTree($data, $rootID = 0)
        {
            $tree = [];
            foreach ($data as $id => $node) {
                if ($node['parent_id'] == $rootID) {
                    unset($data[$id]);
                    $node['childs'] = $this->buildTree($data, $node['id']);
                    $tree[] = $node;
                }
            }
            return $tree;
        }
    
        /**
         * Получаю все категории
         *
         * @return array
         */
        public function getAllCategories()
        {
            $data = Category::find()->asArray()->all();
            return $this->buildTree($data);
        }


    в controller
    $tree = $model->getAllCategories();
    return $this->render('profile', ['tree' => $tree]);


    во view
    foreach ($tree as $cat) {
    // root 1-й уровень
        echo $cat['title'];
        if ($cat['childs'] > 0) {
            foreach ($cat['childs'] as $childs) {
    // category 2-й уровень
                if (empty($childs['childs'])) {
                    echo  $childs['title'];
                } else {
                    echo '<b><br/>' . $childs['title'] . '</b><br/>';
                }
                foreach ($childs['childs'] as $child) {
    // category 3-й уровень
                    echo $child['title'];
                }
            }
        }
        echo '</div>';
    }
    Ответ написан
    3 комментария