/** * "Ленивая загрузка" данных без использования связи * * @param $collection * @param $key * @param $relation_array * @return mixed */ public static function custom_load_hasOne($collection, $key, $relation_array, Closure $additional_rules = NULL) { $model = $relation_array[0]; $local_id = $relation_array[1]; $remote_id = $relation_array[2]; $list = self::makeLists($collection, null, $local_id); #Helper::d($list); $values = new Collection(); if (count($list)) { $values = $model::whereIn($remote_id, $list); if (is_callable($additional_rules)) { #$values = $additional_rules($values); /** * Правильный способ применения доп. условий через функцию-замыкание */ call_user_func($additional_rules, $values); } $values = $values->get(); $values = Dic::modifyKeys($values, 'id'); #Helper::tad($values); } foreach ($collection as $e => $element) { if (isset($element->{$local_id}) && isset($values[$element->{$local_id}])) { /** * Правильная кастомная установка поля. * Доп. поле должно устанавливаться как связь (relation) */ $element->relations[$key] = @$values[$element->{$local_id}] ?: NULL; /** * Правильное обновление значения элемента коллекции */ $collection->put($e, $element); } } unset($list); unset($values); return $collection; }
<?php return array('fields' => function () { /** * Предзагружаем нужные словари с данными, по системному имени словаря, для дальнейшего использования. * Делается это одним SQL-запросом, для снижения нагрузки на сервер БД. */ $dics_slugs = array('city', 'course'); $dics = Dic::whereIn('slug', $dics_slugs)->with('values')->get(); $dics = Dic::modifyKeys($dics, 'slug'); #Helper::tad($dics); $lists = Dic::makeLists($dics, 'values', 'name', 'id'); #Helper::dd($lists); $lists_ids = Dic::makeLists($dics, null, 'id', 'slug'); #Helper::dd($lists_ids); return array('company' => array('title' => 'Компания', 'type' => 'text'), 'position' => array('title' => 'Должность', 'type' => 'text'), 'quote' => array('title' => 'Цитата в шапке', 'type' => 'textarea'), 'header_img' => array('title' => 'Изображение в шапке', 'type' => 'image', 'params' => array('maxFilesize' => 4)), 'avatar' => array('title' => 'Аватар', 'type' => 'image', 'params' => array('maxFilesize' => 4)), 'gender' => array('title' => 'Пол', 'type' => 'select', 'values' => Config::get('site.genders')), 'short' => array('title' => 'Короткий текст', 'type' => 'textarea'), 'full' => array('title' => 'Полный текст', 'type' => 'textarea_redactor'), 'city_id' => array('title' => 'Город', 'type' => 'select', 'values' => $lists['city'], 'default' => Input::get('filter.fields.city_id') ?: null), 'course_id' => array('title' => 'Обучался на курсе', 'type' => 'select', 'values' => array('Выберите..') + $lists['course'], 'default' => Input::get('filter.fields.course_id') ?: null), '-' => array('type' => 'custom', 'content' => '<br/><strong>ИЛИ</strong><br/><br/>'), 'course_text' => array('title' => 'Название курса', 'type' => 'text'), 'fb_link' => array('title' => 'Facebook', 'type' => 'text', 'others' => ['placeholder' => 'http://']), 'vk_link' => array('title' => 'VKontakte', 'type' => 'text', 'others' => ['placeholder' => 'http://']), 'ig_link' => array('title' => 'Instagram', 'type' => 'text', 'others' => ['placeholder' => 'http://']), 'tw_link' => array('title' => 'Twitter', 'type' => 'text', 'others' => ['placeholder' => 'http://']), 'yt_link' => array('title' => 'YouTube', 'type' => 'text', 'others' => ['placeholder' => 'http://']), 'mainpage' => array('no_label' => true, 'title' => 'Показывать на главной', 'type' => 'checkbox', 'label_class' => 'normal_checkbox')); }, 'hooks' => array('after_store_update_destroy_order' => function ($dic = NULL, $dicval = NULL) { Cache::forget('dic_' . $dic->slug); }));
public function manage($id) { Allow::permission($this->module['group'], $this->module['name']); $element = Storage::find($id); if (!is_object($element)) { App::abort(404); } $element->extract(); #Helper::tad($element); #Helper::dd($element->items->{5}->title); /* Helper::dd( StringView::make( array( 'template' => $element->items->{5}->title, 'cache_key' => sha1($element->items->{5}->title), 'updated_at' => time() ), array('key' => 'val') )->render() ); */ #Helper::dd(StringView::force($element->items->{5}->title)); $pages = Page::where('version_of', NULL)->orderBy('created_at', 'DESC')->get(); $pages = Dic::modifyKeys($pages, 'id'); #Helper::ta($pages); /** * Убираем из набора ссылок меню те страницы, которых несуществует (скорее всего они были удалены) */ #/* if (isset($element->items) && count($element->items) && $pages !== NULL && count($pages)) { foreach ($element->items as $i => $item) { if (!is_object($item) || $item->type != 'page') { continue; } #Helper::d($i); #Helper::d($item); if (!isset($pages[$item->page_id])) { unset($element->items->{$i}); } } #Helper::tad($element); } #*/ $functions = array(); $temp = Config::get('menu.functions'); if (count($temp)) { foreach ($temp as $name => $closure) { $result = $closure(); $functions[$name] = is_array($result) && isset($result['text']) ? @$result['text'] . " (" . $name . ")" : $name; } } return View::make($this->module['tpl'] . 'manage', compact('element', 'pages', 'functions')); }