I am unable to save data in related tables
I have tables
ProductsTable
ProductsPricesTable
in ProductsTable table I have declared a relation like
public function initialize( array $config ) {
parent::initialize($config);
...
$this->hasMany('ProductsPrices');
}
and in ProductsController's add action I have something like
if( $this->request->is('post') ) {
$this->Products->patchEntity( $entity, $this->request->data, [
'associated' => ['ProductsPrices']
]);
if( $this->Products->save( $entity ) ) {
....
....
}
}
and the request->data looks like
[
'category_id' => '68',
'code' => '20',
'name' => 'Tagliatelle Bolognese',
'description' => 'mit Fleischsauce ',
'order' => '20',
'active' => '1',
'offered' => '0',
'productsprices' => [
(int) 0 => [
'size_id' => '80',
'price' => '7'
]
]
]
it saves only product in prodcts table, but in products_prices table is nothing saved. whose structure looks like
id | product_id | size_id | price | created | modified
Any idea?
Related
I have a table that looks like this:
,====,==============,============,==========,
| id | contact_from | contact_to | message |
|====|==============|============|==========|
| 1 | 1 | 2 | some msg |
| 2 | 2 | 1 | reply |
'----'--------------'------------'----------'
I create a new row, doing this:
public function add()
{
$message = $this->Messages->newEntity();
if ($this->request->is('post') && $this->request->is('ajax')) {
$data = $this->request->getData();
$data['contact_to'] = (int)$data['contact_to'];
$data['contact_from'] = (int)$this->Auth->user('id');
$message = $this->Messages->patchEntity($message, $data);
if ($this->Messages->save($message)) {
echo json_encode(['status' => 'success']);
exit;
}
echo json_encode(['status' => 'error']);
exit;
}
}
And this is my hasOne association:
$this->hasOne('ContactFrom', [
'className' => 'Contacts',
'foreignKey' => 'id',
'bindingKey' => 'contact_from',
'joinType' => 'INNER',
'propertyName' => 'contact_from'
]);
$this->hasOne('ContactTo', [
'className' => 'Contacts',
'foreignKey' => 'id',
'bindingKey' => 'contact_to',
'joinType' => 'INNER',
'propertyName' => 'contact_to'
]);
As you can see, I pass an ID to a new row, however it saves everything, except the id's. When I debug($message) after the patchEntity call, it comes back like this:
object(App\Model\Entity\Message) {
'message' => 'asdfasdf',
'date_sent' => object(Carbon\Carbon) {},
'[new]' => true,
'[accessible]' => [
'contact_to' => true,
'contact_from' => true,
'message' => true,
],
'[dirty]' => [
'message' => true,
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Messages'
}
It drops my ID's. I assume it's because I need to pass the Entity to it, but to save on db calls, how can I make it save the contact_to and contact_from id's to the table?
The names that you've choosen are causing a clash in the marshaller.
You cannot use the same name for the binding/foreign key and the property name, these two need to be different, as the former are ment to hold an identifier, and the latter is ment to hold either an entity, or an array that can be marshalled into an entity - neither of that applies to the value that you are passing, hence it will be discard.
You should ideally follow the CakePHP naming conventions, and append _id to your columns, ie name them contact_from_id and contact_to_id.
See also
Cookbook > CakePHP at a Glance > CakePHP Conventions > Database Conventions
How can I filter data-set in 2 Tables where second table have more results 1:n.
In first table I can use orWhere and I am getting right data, but my another Table contain multiple results and if I use contain or matching I am getting only data from second Table.
So, I want to filter both tables and get matched data.
Here is my query:
First query to filter first table
$query
->where([
'OR' => [
'Orders.id' => $freeText,
'Orders.postal' => $freeText,
'Orders.address LIKE' => '%' . $freeText . '%',
'Orders.city LIKE' => '%' . $freeText . '%',
'Users.first_name' => $freeText,
'Users.last_name' => $freeText,
'ProjectAddresses.cost_centre' => $freeText,
'CONCAT(first_name, last_name) LIKE' => '%' . str_replace(' ', '', $freeText) . '%',
'CONCAT(first_name, last_name) LIKE' => '%' . $freeText . '%',
'Users.first_name IN ' => $splittedKeywords,
'Users.last_name IN ' => $splittedKeywords,
]
]);
Second query - try to filter data from second Table (but still need matched data from first table)
$query->contain('Items', function ($q) use ($freeText) {
return $q->where(['vessel_id' => $freeText]);
});
So problem is if I use second query he automatically take only data from second table and my goal is to get all filtered data (from first and second table).
I have 20+ data-sets like:
(int) 0 => [
'id' => (int) 1,
'uuid' => '5f34ecda-6bc6-46ed-b5cc-b2227029aed8',
'user_id' => (int) 319,
'status' => (int) 30,
'order_price' => (float) 341.04,
'address_id' => (int) 379,
'address' => 'XYZ',
'building_number' => '171',
'postal' => '111',
'city' => 'XYZ',
'country' => 'AT',
'project_address' => [
'id' => (int) 379,
'type' => 'project',
'group_id' => (int) 3,
'default' => false,
'corresponding_invoice_address' => null,
'short_name' => 'XYT',
'comment' => '',
],
'user' => [
'id' => (int) 319,
'uuid' => '675216eb-7110-44d2-82a7-f7f020e934a6',
'title' => 'Herr',
'first_name' => 'Test',
'last_name' => 'Test',
],
'item_groups' => [],
'items' => [
(int) 0 => [
'id' => (int) 26,
'uuid' => 'f4f629be-e25e-4432-8d97-6b2adcee9065',
'item_group_id' => null,
'type' => (int) 2,
'status' => (int) 30,
'vessel_id' => (int) 40001,
'features' => [],
],
(int) 1 => [
'id' => (int) 28,
'uuid' => 'f4f629be-e25e-4432-8d97-6b2adcee9065',
'item_group_id' => null,
'type' => (int) 2,
'status' => (int) 30,
'vessel_id' => (int) 40003,
'features' => [],
],
(int) 1 => [
'id' => (int) 29,
'uuid' => 'f4f629be-e25e-4432-8d97-6b2adcee9065',
'item_group_id' => null,
'type' => (int) 2,
'status' => (int) 30,
'vessel_id' => (int) 40003,
'features' => [],
],
]
],
SQL
SELECT *
FROM orders Orders
INNER JOIN users Users ON Users.id = (Orders.user_id)
LEFT JOIN addresses ProjectAddresses ON ProjectAddresses.id = (Orders.address_id)
WHERE (Orders.id = :c0
OR Orders.postal = :c1
OR Orders.address LIKE :c2
OR Orders.city LIKE :c3
OR Users.first_name = :c4
OR Users.last_name = :c5
OR ProjectAddresses.cost_centre = :c6
OR CONCAT(first_name, last_name) LIKE :c7
OR Users.first_name IN (:c8)
OR Users.last_name IN (:c9))
Parameter is c1 = 4001 || %40001% || %40001
#ndm The goal is If somebody send in $freeText == 40003 I have to get as result this object where vessel_id = 40003 and thats works, but if somebody send in $freeText == Test then I need again same result because first_name == Test 8see first query) and when in second wuery I am using matching/contain this results are removed because he only "fetch" rows that are matching/contain Items...
Basically I want to check 10+ columns with given $freeText variable and if it is matching I want that results in my data-set (from both tables)
If I understood you correctly, then you're looking for a left join filter, ie left join Items (additionally to containing it for retrieval), group by Orders primary key (to avoid duplicates), and then simply add the Items.vessel_id condition to the main queries WHERE clause in order to make it a OR condition too.
$query
->contain('Items')
->leftJoinWith('Items')
->where([
'OR' => [
'Orders.id' => $freeText,
// ...
'Items.vessel_id' => $freeText
]
])
->groupBy('Orders.id');
See also
Cookbook > Database Access & ORM > Query Builder > Filtering by Associated Data
Cookbook > Database Access & ORM > Query Builder > Using leftJoinWith
I have a set of checkboxes with a belongs to many relationship. I couldnt use a multiple checkbox option for various reasons. The table Tutors has a belongsToMany relationship with subjects table. I get the correct checkboxes checked when it loads and when I update the checboxes I get the correct checkbox data returned but how do i save this? The docs say i need to put the id's in an array for the correct Subjects to be checked . I tried a few things but I couldnt save the data. I dont get an error, just not saving.
//view
foreach ($subjects as $key => $item) {
$sub=$item->name;
$val=0;
foreach ($tutor->subjects as $key2 => $item2) {
$sub2=$item2->name;
if ($sub==$sub2){
$val=1;
break;
}
}
echo $this->Form->hidden('subjects.'.$key.'.id', ['value'=>$item->id]);
echo $this->Form->input('subjects.'.$key.'.checkedvalue', ['label'=>$sub,
'checked'=> $val,'type'=>'checkbox']);
}
//controller
public function edittest5($id = null)
{
$tutor = $this->Tutors->get($id, [
'contain' => ['AvailabilityForTutors','Subjects']
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$tutor = $this->Tutors->patchEntity($tutor, $this->request->data(),['associated' => ['AvailabilityForTutors','Subjects']] );
// $tutor->dirty('availability_for_tutors', true);
debug($tutor);
$this->Tutors->save($tutor,[
// 'atomic'=>false,
'validate' => false,
'associated' => ['Subjects']
]);
return $this->redirect(['action' => 'edittest5',$id]);
}
$subjects = $this->Tutors->Subjects->find('all', ['limit' => 200]);
returned data
'first_name' => 'drew',
'subjects' => [
(int) 0 => [
'id' => '1',
'checkedvalue' => '1'
],
(int) 1 => [
'checkedvalue' => '0'
],
(int) 2 => [
'checkedvalue' => '0'
],
(int) 3 => [
'checkedvalue' => '0'
],
(int) 4 => [
'id' => '5',
'checkedvalue' => '1'
I use BlueImp jQuery plugin to upload multiple images in follow scenario:
User open add or edit Albums page, select images and procces upload via ajax method. This work fine, image data (name, size, model, field,..) are stored in related images table without foreign key.
Ajax return id of recent uploaded image, and js create (captions, position) inputs for that file.
User fill other form fields, and click submit.
Problem, if user create new album or edit/update exist one wich not contain images, application does not update foriegn key for new images. But if album contain images, and user add new one to album, after save post cakephp add / update foreign key to new images records.
Debug output:
// first time add images, does not update FK
[
'name' => 'A4',
'images' => [
(int) 116 => [
'caption' => '',
'position' => '1',
'id' => '116'
]
],
'id' => '4',
'user_id' => '1',
'active' => '1'
]
// album has one image, we add new one, CakePHP Update FK for new images
[
'name' => 'A4',
'images' => [
(int) 117 => [
'caption' => '',
'position' => '1',
'id' => '117'
],
(int) 118 => [
'caption' => '',
'position' => '2',
'id' => '118'
]
],
'id' => '4',
'user_id' => '1',
'active' => '1'
]
AlbumsController add method:
public function add()
{
$album = $this->Albums->newEntity();
if ($this->request->is('post')) {
$album = $this->Albums->patchEntity($album, $this->request->data,['associated' => ['Images']]);
if ($this->Albums->save($album)) {
$this->Flash->success(__('The album has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The album could not be saved. Please, try again.'));
}
}
$users = $this->Albums->Users->find('list', ['limit' => 200]);
$this->set(compact('album', 'users'));
$this->set('_serialize', ['album']);
}
Albums Table:
$this->hasMany('Images', [
'foreignKey' => 'foreign_key',
'conditions' => [
'Images.model' => 'Albums',
'Images.field' => 'upload'
],
'sort' => ['Images.position' => 'ASC']
]);
Images Table:
$this->belongsTo('Albums', [
'foreignKey' => 'foreign_key',
'joinType' => 'INNER'
]);
I use same approach in the cakephp 2 apps, and work without problems.
Video: http://screencast-o-matic.com/watch/cDhYYOinCX
Q: How to update foreignKey?
I'm not sure is this correct way, but it's work for me.
AlbumsTable
public function afterSave(Event $event, Entity $entity, ArrayObject $options)
{
if (isset($entity)) {
$images = TableRegistry::get('Images');
$images->query()
->update()
->set(['foreign_key' => $entity->id])
->where([
'foreign_key IS NULL',
'model' => 'Albums'
])
->execute();// conditions
}
}
Basically, there are 3 models:
Class Model1 {
Public $hasMany = array {
'Model2' => array(
'className' => 'Model2',
'foreignKey' => 'model1id'
)
};
}
Class Model2 {
Public $belongsTo = array {
'Model1' => array(
'className' => 'Model1',
'foreignKey' => 'model1id'
)
};
Public $hasMany = array {
'Model3' => array(
'className' => 'Model3',
'foreignKey' => 'model2id'
)
};
}
Class Model3 {
Public $belongsTo = array {
'Model2' => array(
'className' => 'Model2',
'foreignKey' => 'model2id'
)
};
}
In ctp file under Model1s view folder, I have code as below:
echo $this->Form->create('Model1');
echo $this->Form->input('[some_model1_field]');
echo $this->Form->input('Model2.0.[some_field]');
echo $this->Form->input('Model3.0.[some_field]');
echo $this->Form->end('Save');
In Controller, having a function like below:
$this->Model1->saveAssociated($this->request->data, array('deep' => true));
The result is the all fields are inserted into db, including the foreignkey model1id saved into model2 table automatically by framework. The only issue is that the model2id cannot saved into model3 table automatically by framework. How to deal with it?
Model1 is not associated with Model3, your input field names and the resulting request data however are representing such an association. Debug the request data and compare it to what the docs are showing
http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveassociated-array-data-null-array-options-array
The problem with the structure should be obvious, it's going to be something like
[
// ...
'Model2' => [
[/* ... */]
]
'Model3' => [
[/* ... */]
]
]
If you'd wanted to save Model3s for Model2, then you'd have to properly nest the data like
echo $this->Form->input('Model2.0.Model3.0.[some_field]');
which should result in something like
[
// ...
'Model2' => [
// ...
'Model3' => [
[/* ... */]
]
]
]