I'm trying to insert into order_items table on laravel 5.6 and getting following error
"Array to string conversion (SQL: insert into order_items (title, order_id, quantity, unit_price, unit_booking_fee) values (sector1, 129, 1, 1, 1))"
order_items table schema
Schema::create('order_items', function ($table) {
$table->increments('id');
$table->string('title', 255);
$table->integer('quantity');
$table->decimal('unit_price', 13, 2);
$table->decimal('unit_booking_fee', 13, 2)->nullable();
$table->unsignedInteger('order_id');
$table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
$table->softDeletes();
});
I've tried following both code and both give same error
$orderItem = new OrderItem();
$orderItem->title = $attendee_details['ticket']['title'];
$orderItem->quantity = $attendee_details['qty'];
$orderItem->order_id = $order_id;
$orderItem->unit_price = $attendee_details['ticket']['price']; //0.15
$orderItem->unit_booking_fee = $attendee_details['ticket']['booking_fee'] + $attendee_details['ticket']['organiser_booking_fee']; //0.01
$orderItem->save();
OrderItem::create([
'title' => 'sector1',//$attendee_details['ticket']['title'],
'order_id' => $order_id,
'quantity' => $attendee_details['qty'],
'unit_price' => 1,
'unit_booking_fee' => 1
]);
What am i doing wrong?
Solved it.
Found that the $order_id was an array
$options = array(
'fields' => array(
'item_requirements.*',
'COUNT(`item_requirements`.`quantity_required`) as count'
),
'joins' => array(
'INNER JOIN `items` AS item_requirements ON `item_requirements`.`item_id` = `items`.`id`'
),
'group' => '`item_requirements`.`item_id`',
'contain' => array(
'items' => array('fields' => array('name', 'specification'))
)
);
$query = $this->Indents->ItemRequirements->find('all', $options);
Error: SQLSTATE[42601]: Syntax error: 7 ERROR: zero-length delimited identifier at or near """" LINE 1: ...item_requirements`.`quantity_required`) AS "counts"" AS "COU... ^
the above error occurs.. any solutions to solve this..
The issue is with
item_requirements.*.
.* is not supported by cake ORM.
You can use ->autoFields(true) option. Refer this answer
I have the following query and it is not working as expected.
$students = StudentStatus::with(['user.studentProgramme' => function ($query) {
$query->where('department_course_id', request('studyCourse'));
}], 'level', 'user.studentProgramme.course')
->where('level_id', request('level'))
->where('status', 0)
->get();
The inner WHERE, that is $query->where('department_course_id', request('studyCourse')) is ignored and I don't know why.
Is there something I am missing out?
This may be a scope issue try:
$studyCourse = ;
$students = StudentStatus::with(['user' => function($query) {
$query->with(['studentProgramme' => function ($query) {
$query->where('department_course_id', request('studyCourse'));
}]}], 'level', 'user.studentProgramme.course')
->where('level_id', request('level'))
->where('status', 0)
->get();
Community,
I'm currently facing an issue with the containable-behavior setting conditions based on the datasources expression-builder. I'm using CakePHP 2.6.2 with a PostgreSQL database.
What works so far:
I wrote a behavior that dynamically adds conditions to find-operations to restrict the results based on a privileges table. Im using subqueries with the buildstatement() and expression() functions provided by cake. I followed this article from the CakeBook:
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html
Here is a simplyfied code-snippet, in fact its two OR-statements:
$conditionsSubQueryRecord = array(
'Privilege.objecttable' => $model->table,
'Privilege.objectid = '.$model->alias.'.'.$model->primaryKey,
'Privilege.read' => true,
'Privilege.id' => $this->recordPermissions
);
$dsPrivilege = $this->privilegeModel->getDataSource();
$subQueryRecordPrivs = $dsPrivilege->buildStatement(
array(
'fields' => array('"'.$this->privilegeModel->alias.'"."id"'),
'table' => $dsPrivilege->fullTableName($this->privilegeModel),
'alias' => $this->privilegeModel->alias,
'limit' => null,
'offset' => null,
'joins' => array(),
'conditions' => $conditionsSubQueryRecord,
'order' => null,
'group' => null
),
$this->privilegeModel
);
$subQueryRecordPrivs = ' EXISTS (' . $subQueryRecordPrivs . ') ';
$subQueryRecordPrivsExpression = $dsPrivilege->expression($subQueryRecordPrivs);
I'm adding the statement to my condition array then in my behaviors beforeFind()-hook. This works all very well so far. The condition is added, the results are filtered.
The conditions are ignored for my contained models:
My problem is now to use this condition on contained models. I wrote an recursive algorithm that walks along all the contained modelsand if the model actsAs my behavior I am attaching the same conditions to its conditions-array. But when I execute my search, the condition is ignored on the contained models and only attached to the primary model.
This is the complete condition string I'm executing:
array(
'conditions' => array(
'Requestinstance.id' => (int) 4,
(int) 0 => object(stdClass) {
type => 'expression'
value => ' EXISTS (SELECT "Privilege"."id" FROM "core"."privileges" AS "Privilege" WHERE "Privilege"."objecttable" = 'requestinstances' AND "Privilege"."objectid" = "Requestinstance"."id" AND "Privilege"."read" = 'TRUE' AND "Privilege"."id" = (8)) OR EXISTS (SELECT "Privilege"."id" FROM "core"."privileges" AS "Privilege" WHERE "Privilege"."objecttable" = 'requestinstances' AND "Privilege"."read" = 'TRUE' AND "Privilege"."id" IN (7, 13, 6, 9, 10, 12) AND "Privilege"."objectid" IS NULL) '
}
),
'fields' => null,
'joins' => array(),
'limit' => (int) 1,
'offset' => null,
'order' => array(
(int) 0 => null
),
'page' => (int) 1,
'group' => null,
'callbacks' => true,
'contain' => array(
'Requesttype' => array(
'Steptype' => array(
'order' => array(
(int) 0 => 'RequesttypesSteptype.phase ASC'
),
'conditions' => object(stdClass) {
type => 'expression'
value => ' EXISTS (SELECT "Privilege"."id" FROM "core"."privileges" AS "Privilege" WHERE "Privilege"."objecttable" = 'steptypes' AND "Privilege"."objectid" = "Steptype"."id" AND "Privilege"."read" = 'TRUE' AND "Privilege"."id" = (8)) OR EXISTS (SELECT "Privilege"."id" FROM "core"."privileges" AS "Privilege" WHERE "Privilege"."objecttable" = 'steptypes' AND "Privilege"."read" = 'TRUE' AND "Privilege"."id" IN (7, 13, 6, 9, 10, 12) AND "Privilege"."objectid" IS NULL) '
}
),
(int) 0 => 'RequesttypesSteptype',
'conditions' => object(stdClass) {
type => 'expression'
value => ' EXISTS (SELECT "Privilege"."id" FROM "core"."privileges" AS "Privilege" WHERE "Privilege"."objecttable" = 'requesttypes' AND "Privilege"."objectid" = "Requesttype"."id" AND "Privilege"."read" = 'TRUE' AND "Privilege"."id" = (8)) OR EXISTS (SELECT "Privilege"."id" FROM "core"."privileges" AS "Privilege" WHERE "Privilege"."objecttable" = 'requesttypes' AND "Privilege"."read" = 'TRUE' AND "Privilege"."id" IN (7, 13, 6, 9, 10, 12) AND "Privilege"."objectid" IS NULL) '
}
),
'Stepinstance' => array(
(int) 0 => 'Steptype',
(int) 1 => 'Stepdatainstance',
(int) 2 => 'Sectioninstance'
),
'Requestdatainstance' => array(),
'Taskinstance' => array()
),
'recursive' => (int) 2
)
As you can see, the condition was correctly added to some of the contained models. However, the executed SQL-query, i.e. for the "Steptype"-Model, is generated without the condition:
SELECT "Steptype"."id" AS "Steptype__id", "Steptype"."name" AS "Steptype__name", "Steptype"."description" AS "Steptype__description", "Steptype"."subscribe" AS "Steptype__subscribe", "RequesttypesSteptype"."id" AS "RequesttypesSteptype__id", "RequesttypesSteptype"."phase" AS "RequesttypesSteptype__phase", "RequesttypesSteptype"."endsphase" AS "RequesttypesSteptype__endsphase", "RequesttypesSteptype"."endsrequest" AS "RequesttypesSteptype__endsrequest", "RequesttypesSteptype"."usertype_id" AS "RequesttypesSteptype__usertype_id", "RequesttypesSteptype"."requesttype_id" AS "RequesttypesSteptype__requesttype_id", "RequesttypesSteptype"."steptype_id" AS "RequesttypesSteptype__steptype_id" FROM "core"."steptypes" AS "Steptype" JOIN "core"."requesttypes_steptypes" AS "RequesttypesSteptype" ON ("RequesttypesSteptype"."requesttype_id" = 6 AND "RequesttypesSteptype"."steptype_id" = "Steptype"."id") ORDER BY "RequesttypesSteptype"."phase" ASC
Direct use of the buildStatement does not work either
I also tried to use the statement itself directly, without building an expression from it. This actually creates exactly the SQL-query I want to have, but does not add the quotes of the table alias in the FROM-clause correctly and therefore causes postgreSQL to throw an error:
SELECT "Requestinstance"."id" AS "Requestinstance__id", "Requestinstance"."user_id" AS "Requestinstance__user_id", "Requestinstance"."created" AS "Requestinstance__created", "Requestinstance"."requesttype_id" AS "Requestinstance__requesttype_id", "Requestinstance"."currentphase" AS "Requestinstance__currentphase", "Requestinstance"."selfsolving" AS "Requestinstance__selfsolving", "User"."username" AS "User__username", "User"."id" AS "User__id", "User"."company_id" AS "User__company_id", "User"."usertype_id" AS "User__usertype_id", "Requesttype"."id" AS "Requesttype__id", "Requesttype"."name" AS "Requesttype__name", "Requesttype"."subtitle" AS "Requesttype__subtitle", "Requesttype"."description" AS "Requesttype__description", "Requesttype"."order" AS "Requesttype__order", "Requesttype"."selfsolving" AS "Requesttype__selfsolving" FROM "core"."requestinstances" AS "Requestinstance" LEFT JOIN "core"."users" AS "User" ON ("Requestinstance"."user_id" = "User"."id") LEFT JOIN "core"."requesttypes" AS "Requesttype" ON ("Requestinstance"."requesttype_id" = "Requesttype"."id") WHERE EXISTS (SELECT "Privilege"."id" FROM "core"."privileges" AS Privilege WHERE "Privilege"."objecttable" = 'requestinstances' AND "Privilege"."objectid" = "Requestinstance"."id" AND "Privilege"."read" = 'TRUE' AND "Privilege"."id" = (8)) OR EXISTS (SELECT "Privilege"."id" FROM "core"."privileges" AS Privilege WHERE "Privilege"."objecttable" = 'requestinstances' AND "Privilege"."read" = 'TRUE' AND "Privilege"."id" IN (7, 13, 6, 9, 10, 12) AND "Privilege"."objectid" IS NULL) LIMIT 1
Adding the quotes manually to the alias-string while building the statement does not help either, since the framework strips the quotes.
So finally my question(s):
Does anybody know, if the containable-behavior supports expressions at all? I already digged into the DboSource, PdoSource and Postgresql-datasource but could not find anything wrong here. The Containable behavior looks pretty straight forward as well. Am I doing something wrong here?
Or is there another way I could acchieve what I want?
I'm glad for any help in this matter!
Thanks in advance!
I finally figured it out!
To formally answer the question:
Yes, the containable behavior does support expression-syntax!
My problem was with the processing order: The framework processes the behaviors the configured order, I accidently loaded the containable before my custom behavior, thats why it never received my modified conditions...
Working with the correct order, the manipulated condition-string was processed fine by the containable behavior.
To be absolutely sure about the behaviors order, I moved the behavior-loading to the AppModels __construct() method:
// unload any configured Containable behavior
if($this->Behaviors->loaded('Containable')) {
$this->Behaviors->unload('Containable');
}
// load the PrivilegeItem behavior
if($this->alias !== 'Privilege') {
$this->Behaviors->load('PrivilegeItem');
}
// and finally (re-)attach the Containable behavior
$this->Behaviors->load('Containable');
Maybe it helps others avoiding two days of debugging headache...
I'm getting an error with a query, my question is: can i chain joins?
My first join is to the primary table, but my second join is to the table joined to the primary table. This is the query:
$query = $this->getDbTable()->select()
->from(array('ca' => 'contracts_allotment'),
array('id',
'contracts_rooms_id' => new Zend_Db_Expr("CONCAT(room_type_desc, '-', room_characteristics_desc)")
))
->join(array('cr' => 'contracts_rooms'),
'ca.contract_rooms_id = cr.id',
array())
->join(array('rt' => 'room_types'),
'cr.room_id = rt.id',
array('room_type_desc'))
->join(array('rc' => 'room_characteristics'),
'cr.char_id = rc.id',
array('room_characteristics_desc'))
->where('contract_id = ?', $contractId);
var_dump($this->getDbTable()->fetchAll($query));die;
I'm getting:
Select query cannot join with another table"
The error comes from Zend/Db/Table/Select::assemble()
Here you have some inside assemble():
// Check each column to ensure it only references the primary table
if ($column) {
if (!isset($from[$table]) || $from[$table]['tableName'] != $primary) {
var_dump($from[$table]['tableName'], $primary);die;
require_once 'Zend/Db/Table/Select/Exception.php';
throw new Zend_Db_Table_Select_Exception('Select query cannot join with another table');
}
}
The var_dump() prints:
string(10) "room_types" string(19) "contracts_allotment"
Any idea?
Don't forget to lock the tables when doing joins:
$query = $this->getDbTable()->select()
->setIntegrityCheck(false)
->from(array('ca' => 'contracts_allotment'),
array('id',
'contracts_rooms_id' => new Zend_Db_Expr("CONCAT(room_type_desc, '-', room_characteristics_desc)")
))
->join(array('cr' => 'contracts_rooms'),
'ca.contract_rooms_id = cr.id',
array())
->join(array('rt' => 'room_types'),
'cr.room_id = rt.id',
array('room_type_desc'))
->join(array('rc' => 'room_characteristics'),
'cr.char_id = rc.id',
array('room_characteristics_desc'))
->where('contract_id = ?', $contractId);
->setIntegrityCheck(false) should at least get you a new error.