Построитель запросов QueryBuilder

#
Re: Построитель запросов QueryBuilder
При попытке вставки значений, одним из которых является объект Core_QueryBuilder::expression(), в таблицу, что-то идет не так.

Запрос такого плана:

$oQuery_Builder_Insert = Core_QueryBuilder::insert( 'tableName', $array )->execute();


Получаю сообщение об ошибке.

Exception: Wrong argument type 'object' (expected string) for quote()

Ранее мы подобную проблему уже обсуждали здесь, только вот это было для UPDATE. Где искать корень неудачи?
Модератор
#
Re: Построитель запросов QueryBuilder
var_dump($array); покажите
#
Re: Построитель запросов QueryBuilder
Собственно, само предшествующее выражение...

$this->_aData[ 'client_visit_date' ] = Core_QueryBuilder::expression( 'FROM_UNIXTIME( ' . $unix_visitDate .  ' )' );


А вот var_dump

array(12) {
  ["client_name"]=>
  string(12) "Имя"
  ["client_fathername"]=>
  string(20) "Отчество"
  ["client_surname"]=>
  string(22) "Фамилия"
  ["client_visit_date"]=>
  object(Core_QueryBuilder_Expression)#102 (2) {
    ["_expression":protected]=>
    string(27) "FROM_UNIXTIME( 1445893200 )"
    ["_dataBase":protected]=>
    object(Core_DataBase_Mysql)#4 (8) {
      ["_result":protected]=>
      NULL
      ["_quoteColumnNameCache":protected]=>
      array(40) {
        ["constants"]=>
        string(11) "`constants`"
        ["constants.*"]=>
        string(13) "`constants`.*"
        ["active"]=>
        string(8) "`active`"
        ["deleted"]=>
        string(9) "`deleted`"
        ["constants.deleted"]=>
        string(21) "`constants`.`deleted`"
        ["value"]=>
        string(7) "`value`"
        ["sessions"]=>
        string(10) "`sessions`"
        ["id"]=>
        string(4) "`id`"
        ["time"]=>
        string(6) "`time`"
        ["modules"]=>
        string(9) "`modules`"
        ["modules.*"]=>
        string(11) "`modules`.*"
        ["modules.deleted"]=>
        string(19) "`modules`.`deleted`"
        ["sorting"]=>
        string(9) "`sorting`"
        ["modules.sorting"]=>
        string(19) "`modules`.`sorting`"
        ["COUNT(*)"]=>
        string(8) "COUNT(*)"
        ["count"]=>
        string(7) "`count`"
        ["sites"]=>
        string(7) "`sites`"
        ["sites.deleted"]=>
        string(17) "`sites`.`deleted`"
        ["site_aliases"]=>
        string(14) "`site_aliases`"
        ["site_aliases.*"]=>
        string(16) "`site_aliases`.*"
        ["site_id"]=>
        string(9) "`site_id`"
        ["site_aliases.site_id"]=>
        string(24) "`site_aliases`.`site_id`"
        ["sites.id"]=>
        string(12) "`sites`.`id`"
        ["name"]=>
        string(6) "`name`"
        ["site_aliases.name"]=>
        string(21) "`site_aliases`.`name`"
        ["site_aliases.deleted"]=>
        string(24) "`site_aliases`.`deleted`"
        ["ipaddresses"]=>
        string(13) "`ipaddresses`"
        ["ip"]=>
        string(4) "`ip`"
        ["ipaddresses.deleted"]=>
        string(23) "`ipaddresses`.`deleted`"
        ["structures"]=>
        string(12) "`structures`"
        ["structures.site_id"]=>
        string(22) "`structures`.`site_id`"
        ["path"]=>
        string(6) "`path`"
        ["parent_id"]=>
        string(11) "`parent_id`"
        ["structures.deleted"]=>
        string(22) "`structures`.`deleted`"
        ["templates"]=>
        string(11) "`templates`"
        ["templates.id"]=>
        string(16) "`templates`.`id`"
        ["templates.deleted"]=>
        string(21) "`templates`.`deleted`"
        ["libs"]=>
        string(6) "`libs`"
        ["libs.id"]=>
        string(11) "`libs`.`id`"
        ["libs.deleted"]=>
        string(16) "`libs`.`deleted`"
      }
      ["_config":protected]=>
      array(8) {
        ["driver"]=>
        string(5) "mysql"
        ["host"]=>
        string(15) "localhost.mysql"
        ["username"]=>
        string(15) "user_mysql"
        ["password"]=>
        string(8) "asir!7er"
        ["database"]=>
        string(18) "dbase"
        ["charset"]=>
        string(4) "UTF8"
        ["newlink"]=>
        bool(false)
        ["attr"]=>
        array(0) {
        }
      }
      ["_connection":protected]=>
      resource(27) of type (mysql link)
      ["_lastQuery":protected]=>
      string(74) "SELECT
*
FROM `libs`
WHERE  `libs`.`id` = '84' AND `libs`.`deleted` = 0"
      ["_queryType":protected]=>
      int(0)
      ["_asObject":protected]=>
      bool(false)
      ["_separator":protected]=>
      array(5) {
        [0]=>
        string(1) "."
        [1]=>
        string(3) " + "
        [2]=>
        string(3) " - "
        [3]=>
        string(3) " / "
        [4]=>
        string(3) " * "
      }
    }
  }
}
Модератор
#
Re: Построитель запросов QueryBuilder
lezhenkin,
напишите, пожалуйста, запрос в поддержку со ссылкой на эту тему, будем решать.
#
Re: Построитель запросов QueryBuilder
Что не так делаю в запросе?
выдается ошибка:  syntax error, unexpected T_VARIABLE в стоке
$oCore_QueryBuilder_Select2 = Core_QueryBuilder::select('properties.id')

Цель: Выводить элементы ИС только на тех страницах, id которых есть в доп. свойствах данного элемента.

код:      
    $Informationsystem_Controller_Show = new Informationsystem_Controller_Show(
            Core_Entity::factory('Informationsystem',87)
         );
         $Informationsystem_Controller_Show
            ->xsl(
               Core_Entity::factory('Xsl')->getByName('СписокЭлементовPUBL_second')
            )
            ->groupsMode('tree')
            ->itemsForbiddenTags(array('text'))
            ->group(669, 671)
            ->limit(2)
                ->itemsProperties(TRUE)
              ->itemsPropertiesList(FALSE);


  // Объединение с нужной таблицей свойств
            
          $Informationsystem_Controller_Show
            ->informationsystemItems()
            ->queryBuilder()
            ->leftJoin('property_value_strings', 'informationsystem_items.id', '=', 'property_value_strings.entity_id',
                
                array(
                array('AND' => array('properties.tag_name', '=', Core_QueryBuilder::expression('`property_value_strings`.`property_id`')))
             )
          )

     // echo Core_DataBase::instance()->getLastQuery();  
    
$oCore_QueryBuilder_Select2 = Core_QueryBuilder::select('properties.id')
            ->from('properties')
            ->where('property_value_strings_value', '=', 'listIdStructure');
// Идентификатор дополнительного свойства
               ->where('properties.tag_name', '=', 594)
            ->where('property_value_strings.property_id', 'IN', $oCore_QueryBuilder_Select2)
             // Количество свойств
               ->having(Core_Querybuilder::expression('COUNT(DISTINCT `informationsystem_item_properties`.`property_id`)'), '=', 1);
         

$Informationsystem_Controller_Show->show();
Модератор
#
Re: Построитель запросов QueryBuilder
Точки с запятыми теряете!
          $Informationsystem_Controller_Show
            ->informationsystemItems()
            ->queryBuilder()
            ->leftJoin('property_value_strings', 'informationsystem_items.id', '=', 'property_value_strings.entity_id',
                
                array(
                array('AND' => array('properties.tag_name', '=', Core_QueryBuilder::expression('`property_value_strings`.`property_id`')))
             )
          );
#
Re: Построитель запросов QueryBuilder
Да, с запятой промахнулась, спасибо. Исправила, но ошибка все равно имеется:

Undefined variable: oCore_QueryBuilder_Select2 в строке ->where('property_value_strings.property_id', 'IN', $oCore_QueryBuilder_Select2)

и еще
Exception: Query error 1054: Unknown column 'properties.tag_name' in 'on clause'. Query: SELECT SQL_CALC_FOUND_ROWS `informationsystem_items`.* FROM `informationsystem_items` LEFT OUTER JOIN `property_value_strings` ON `informationsystem_items`.`id` = `property_value_strings`.`entity_id` AND `properties`.`tag_name` = `property_value_strings`.`property_id` WHERE
Модератор
#
Re: Построитель запросов QueryBuilder
Верно. Почему у вас:

oCore_QueryBuilder_Select2 = Core_QueryBuilder::select('properties.id')
            ->from('properties')
            ->where('property_value_strings_value', '=', 'listIdStructure');


В середине другого ->queryBuilder()?

Как минимум так:

<?php

    $Informationsystem_Controller_Show = new Informationsystem_Controller_Show(
            Core_Entity::factory('Informationsystem',87)
         );
         $Informationsystem_Controller_Show
            ->xsl(
               Core_Entity::factory('Xsl')->getByName('СписокЭлементовPUBL_second')
            )
            ->groupsMode('tree')
            ->itemsForbiddenTags(array('text'))
            ->group(669, 671)
            ->limit(2)
            ->itemsProperties(TRUE)
            ->itemsPropertiesList(FALSE);


  // Объединение с нужной таблицей свойств
  
      $oCore_QueryBuilder_Select2 = Core_QueryBuilder::select('properties.id')
            ->from('properties')
            ->where('property_value_strings_value', '=', 'listIdStructure');
            
          $Informationsystem_Controller_Show
            ->informationsystemItems()
            ->queryBuilder()
            ->leftJoin('property_value_strings', 'informationsystem_items.id', '=', 'property_value_strings.entity_id',
                
                array(
                array('AND' => array('properties.tag_name', '=', Core_QueryBuilder::expression('`property_value_strings`.`property_id`')))
             )
          )

      // echo Core_DataBase::instance()->getLastQuery();  
    
      // Идентификатор дополнительного свойства
      ->where('properties.tag_name', '=', 594)
      ->where('property_value_strings.property_id', 'IN', $oCore_QueryBuilder_Select2)
       // Количество свойств
      ->having(Core_Querybuilder::expression('COUNT(DISTINCT `informationsystem_item_properties`.`property_id`)'), '=', 1);
        

$Informationsystem_Controller_Show->show();
Вы только что начали читать предложение, чтение которого вы уже заканчиваете.
#
Re: Построитель запросов QueryBuilder
ошибка остается,  что не так сортирую?

Exception: Query error 1054: Unknown column 'properties.tag_name' in 'where clause'. Query: SELECT SQL_CALC_FOUND_ROWS `informationsystem_items`.* FROM `informationsystem_items` LEFT OUTER JOIN `property_value_strings` ON `informationsystem_items`.`id` = `property_value_strings`.`entity_id` AND `properties`.`tag_name` = `property_value_strings`.`property_id` WHERE `informationsystem_items`.`informationsystem_id` = '87' AND ( `informationsystem_items`.`start_datetime` < '2015-12-09 15:29:08' OR `informationsystem_items`.`start_datetime` = '0000-00-00 00:00:00' ) AND ( `informationsystem_items`.`end_datetime` > '2015-12-09 15:29:08' OR `informationsystem_items`.`end_datetime` = '0000-00-00 00:00:00' ) AND `informationsystem_items`.`siteuser_group_id` IN (0, -1) AND `properties`.`tag_name` = 594 AND `property_value_strings`.`property_id` IN (SELECT `properties`.`id` FROM `properties` WHERE `property_value_strings_value` = 'listIdStructure') AND `informationsystem_items`.`active` = 1 AND `informationsystem_items`.`informationsystem_group_id` = 669 AND `informationsystem_items`.`deleted` = 0 HAVING COUNT(DISTINCT `informationsystem_item_properties`.`property_id`) = 1 ORDER BY `informationsystem_items`.`datetime` DESC, `informationsystem_items`.`sorting` DESC, `informationsystem_items`.`sorting` ASC LIMIT 2 OFFSET 0


#
Re: Построитель запросов QueryBuilder
Вот переделала так:

$Informationsystem_Controller_Show = new Informationsystem_Controller_Show(
         Core_Entity::factory('Informationsystem', 87)
      );
$Informationsystem_Controller_Show
->xsl(
   Core_Entity::factory('Xsl')->getByName('СписокЭлементовPUBL_second')
   )
->groupsMode('tree')
->group(FALSE)
->limit(10)
->itemsProperties(TRUE)
->itemsPropertiesList(FALSE);
        
  // Объединение с нужной таблицей свойств


     $oCore_QueryBuilder_Select2 = Core_QueryBuilder::select('properties.id')
            ->from('properties')
            ->where('properties.tag_name', '=', 'listIdStructure');
            
          $Informationsystem_Controller_Show
            ->informationsystemItems()
            ->queryBuilder()
            ->leftJoin('property_value_strings', 'informationsystem_items.id', '=', 'property_value_strings.entity_id',
                
                array(
                array('AND' => array('property_value_strings.property_id', '=', Core_QueryBuilder::expression('`property_value_strings`.`property_id`')))
             )
          )
            // Идентификатор дополнительного свойства
      
            
                ->where('property_value_strings.property_id', '=', 594)
                ->where('property_value_strings.value', '=', 'для каждого элемента свой')
->groupBy('informationsystem_items.id')
             // Количество свойств
         ->having(Core_Querybuilder::expression('COUNT(DISTINCT `informationsystem_item_properties`.`property_id`)'), '=', 1);
         

но все равно, не получается, как сделать ->where('property_value_strings.value', '=', 'для каждого элемента свой') ?
и
как выводить только те элементы из подгрупп, которые содержат в доп. свойстве идентификатор текущей страницы.
Дополнительное свойство - это у меня набор идентификаторов (для каждого элемента свой)

$Informationsystem_Controller_Show->show();
              
Авторизация