Has many save cakePHP - php

I have a situation where I have hasMany relations between two objects. request data looks like this
array(
'Project' => array(
'name' => 'Projekt Y',
'main_contractor' => 'APOS - Zagreb',
'main_contractor_id' => '188',
'arhitect_id' => '20'
),
'ProjectAssociate' => array(
(int) 0 => array(
'project_role_id' => '1',
'Person' => array(
'input' => 'First name, last name',
'firm_id' => ''
)
),
(int) 1 => array(
'project_role_id' => '1',
'Person' => array(
'input' => '',
'firm_id' => ''
)
)
)
)
The problem is, that I can not save this data because validation does not allow Person.input to be empty. Is there a way to cancel the save proces only for ProjectAssociate.1? I want to successfully save the rest of the data

Try to unbind your ProjectAssociate model
// Let's remove the hasMany...
$this->Project->unbindModel(
array('hasMany' => array('ProjectAssociate'))
);
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

Related

How to include multiple keys and values in array

I need some help with a JSON post request, I need to post an array of fields with multiple ids and values. Posting one id and value works but I can't figure out how to include multiple field IDs in this request.
Here is my code:
$appointment = $acuity->request('/appointments', array(
'method' => 'POST',
'data' => array(
'firstName' => 'Bob',
'lastName' => 'Burger',
'email' => 'bob#example.org',
'datetime' => '2016-04-26T19:00:00-0700',
'appointmentTypeID' => 5020,
'fields' => array(
array(
'id' => 4, // Custom field ID 4
'value' => '1600 Pennsylvania Avenue',
'id' => 5, // Custom field ID 5
'value' => '5550 Somewhere St.'
)
)
)
));
I suspect the intent is for this to use nested arrays, like so:
'fields' => array(
array(
'id' => 4, // Custom field ID 4
'value' => '1600 Pennsylvania Avenue',
),
array(
'id' => 5, // Custom field ID 5
'value' => '5550 Somewhere St.'
)
)

Yii1 CGridView(Yii-Booster): How to change filter key(filterVal) in TbGridView(based on CGridView)?

I am using yii-booster(4.0.1) TbGridView(extends CGridView) and need to change the filter variable name in _REQUEST($_POST, $_GET) for filter function.
In my grid, I have filter functionality and when I press enter after entering some words in the filter input, an ajax request will sent for server. in this request in $_REQUEST I have:
array
(
'page' => '1'
'wsi_it_model_Asset' => array
(
'user' => 'eghlima'
'createdAt' => ''
'serial' => ''
'brand' => ''
'model' => ''
'assetType' => ''
'assigned' => ''
'location' => ''
'status' => ''
)
)
My question is how can I change wsi_it_model_Asset in the request created by CGridView.
I know that I should do it through a parameter in CActiveDataProvider when I am creating the dataProvider but I can not find it.
Thanks in advance.
UPDATE 24 Jan
I found my code from another project, as you can see I can change the key for sort and pagination, I need something look like for filtering key;
return new \CActiveDataProvider($this->applicant, array(
'criteria' => $criteria,
'pagination' => array(
'pageVar' => 'p', // <<<<< pagination var
'pageSize' => 20,
),
'sort' => array(
'sortVar' => 's', // <<<<< sorting var
'defaultOrder' => 't.firstName ASC',
'attributes' => array(
'*'
)
),
));
So for pagination, the request which is posting from client to server will be:
array
(
'p' => '7' // <<<<<< page changed to `p`
'wsi_it_model_Asset' => array
(
'user' => 'eghlima'
'createdAt' => ''
'serial' => ''
'brand' => ''
'model' => ''
'assetType' => ''
'assigned' => ''
'location' => ''
'status' => ''
)
)
Im not sure what you are meaning, but I give it a try.
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'my-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
array(
'value' => '$data->theValue',
'filter' => CHtml::activeTextField($model, 'myWish'),
),
)
));
And then in your model you add a virtual attribute
public function getMyWish()
{
return 'Your dream answere';
}
And in your $model->search() add:
$criteria->compare('$data->theValue', $this->myWish,true);
Something like this check a very good tutorial on virtual attributes: http://www.yiiframework.com/wiki/167/understanding-virtual-attributes-and-get-set-methods/

Unique Merge multiple multidimensional array

I have some difficulties to merge many multidimensional array in php. I tried to do it by many way, but each time, I don't get the result wanted. I tried with array_merge(array_unique,...) and in different post I found a way with array_map, but I don't understand everything...
I can have many multi array like below:
array(
(int) 0 => array(
'User' => array(
'username' => 'testje',
'firstname' => 'jean',
'lastname' => 'test'
),
'Calendar' => array(
'period' => 'AM'
),
'Shift' => array(
'name' => 'HV',
'color' => '#b7fa00'
),
'Team' => array(
'name' => 'Proxy_B28'
)
),
(int) 1 => array(
'User' => array(
'username' => 'testje',
'firstname' => 'jean',
'lastname' => 'test'
),
'Calendar' => array(
'period' => 'PM'
),
'Shift' => array(
'name' => 'HV',
'color' => '#b7fa00'
),
'Team' => array(
'name' => 'Proxy_B28'
)
)
)
And I would like to get this kind of array :
array(
'User' => array(
'username' => 'testje',
'firstname' => 'jean',
'lastname' => 'test'
),
'Calendar' => array(
'period' => 'Full day'
),
'Shift' => array(
'name' => 'HV',
'color' => '#b7fa00'
),
'Team' => array(
'name' => 'Proxy_B28'
)
)
Do you have some advices to give me to get this result ?
Thank you very much !
I don't know if the best solution but it seems to work like this, and fastly :
foreach ($users as $k=>$v){
//$r[$k] = array_merge($v,$users[$k]);
//$unique[] = array_map("unserialize", array_unique(array_map("serialize", $users[$k])));
$s[$k] = array(
'username' => $v['User']['username'],
'team' => $v['Team']['name'],
'period' => $v['Calendar']['period']
);
if ($k > 0) {
if (in_array($v['User']['username'],$s[$k])) {
unset($s[$k-1]);
$s[$k] = array(
'username' => $v['User']['username'],
'team' => $v['Team']['name'],
'period' => "FD"
);
}
}
}
Do you have another idea or this one is enough good ?
thank you !

Using Containable behavior to filter results in Cakephp 2.2.4

I'm using containable behavior and the result of my find('all') is:
array(
(int) 0 => array(
'User' => array(
'id' => '106',
'email' => 'daje#daje.it',
'pwd' => '0433c024cb08be13000d59a347e640482843f46f177e95749dc6599c259617fd3491dcb940b47693cbbc7f65a2cc5ef62deca2e600c1be133ad54170f7d1fbd1',
'role_id' => '3',
'active' => '1'
),
'Lead' => array(
'id' => '6'
),
'Estimate' => array(
(int) 0 => array(
'lead_id' => '6',
'Estimate' => array(
(int) 0 => array(
'TOT_count' => '2'
)
)
)
)
)
)
I need to to count how many estimates there are in the lead.
The total (2) is correct, but i see nested 'Estimated' array, why ?
The result i would like to get is:
array(
(int) 0 => array(
'User' => array(
'id' => '106',
'email' => 'daje#daje.it',
'pwd' => '0433c024cb08be13000d59a347e640482843f46f177e95749dc6599c259617fd3491dcb940b47693cbbc7f65a2cc5ef62deca2e600c1be133ad54170f7d1fbd1',
'role_id' => '3',
'active' => '1'
),
'Lead' => array(
'id' => '6'
),
'Estimate' => array(
'TOT_count' => '2'
)
)
)
This is the find:
$options = array(
'contain' => array(
'User',
'Estimate' => array(
'fields' => 'COUNT(*) AS TOT_count'
)
),
'conditions' => array('Lead.id' => 6),
'fields' => 'User.*',
'limit' => 1
);
debug($this->Lead->find('all', $options));
How can i do it?
Thanks!
When you use a "custom" AS statement, in your case TOT_count, Cake will always put this in a result key called 0. You can avoid this by defining TOT_count as a virtualField in your model. That way it will be nested directly under the model name in your resultset.
Secondly, the lead_id is forcedly retrieved, because it is "needed" to make the join with the Lead model. It can not properly retrieve all the data without that piece of information there.

List posts that a user likes in CakePHP app

I have two tables: Posts and Likes
And I'm trying to list the posts that the current logged in user likes. The method looks like the following:
$following = $this->Follower->listFollowing($this->Auth->user('id'));
$this->paginate = array('limit'=>20,'conditions'=>array('Post.id'=>array($following['Follower']['post_id']),'Post.status'=>array(1,2)),'order'=>array('Post.datetime'=>'desc'),
'contain'=>array('User','Answer'=>array('User'),'Tag'));
$this->set('posts',$this->paginate());
So basically what I'm trying to do is first query the following (likes) table for all matching rows to the user and then use this array of post ids in my find query to list posts that were in the query.
The listFollowing method in the Follower model looks like:
public function listFollowing($user_id)
{
return $this->find('all', array(
'conditions' => array('Follower.user_id'=>$user_id)
));
}
I'm currently getting an error like: Undefined index: Follower [APP/Controller/PostsController.php, line 94] So gonna presume that the way I'm trying to pass the list of post ids from the following in the find query is incorrect.
Can anyone help? Thanks
Edit: Doing a debug on $following gives:
array(
(int) 0 => array(
'Follower' => array(
'id' => '4',
'user_id' => '6',
'post_id' => '136'
),
'User' => array(
'password' => '*****',
'id' => '6',
'username' => 'driz',
'email' => '######'
),
'Post' => array(
'id' => '136',
'user_id' => '8',
'datetime' => '2012-09-11 15:49:52',
'modified' => '2012-09-16 15:31:38',
'title' => 'Test Content',
'slug' => 'Test_content',
'content' => 'Test Content',
'status' => '1'
)
),
(int) 1 => array(
'Follower' => array(
'id' => '5',
'user_id' => '6',
'post_id' => '133'
),
'User' => array(
'password' => '*****',
'id' => '6',
'username' => 'driz',
'email' => '######'
),
'Post' => array(
'id' => '134',
'user_id' => '8',
'datetime' => '2012-09-11 15:49:52',
'modified' => '2012-09-16 15:31:38',
'title' => 'Test Content 2',
'slug' => 'Test_content_2',
'content' => 'Test Content 2',
'status' => '1'
)
)
)
Couple of questions:
Did you checked what query is being executed? If it's executed then probably the method is correct.
is $following being populated? what happens if you debug it?
if you are retreiving with 'all' then the result will look like this:
Array(
[0] => Array
(
[ModelName] => Array
(
[id] => 83
[field1] => value1
[field2] => value2
[field3] => value3
)
[AssociatedModelName] => Array
(
[id] => 1
[field1] => value1
[field2] => value2
[field3] => value3
)
)
)
So you'll never be able to find something at $following['Follower']. Check Cake's documentation for this
Based on you comments, if you need a list of id's try this inside your method:
public function listFollowing($user_id)
{
return $this->find('list', array(
'conditions' => array('Follower.user_id'=>$user_id)
'fields' => array('Follower.user_id'),
'recursive' => 0);
}
The 'list' retrieve mode does exactlye that.
It is hard to guess the error with this limited information but I'm guessing that it's a PHP error related with your $following array.
Can you try putting debug($following) after your listFollowing() function call? So in the end, it should look like this:
$following = $this->Follower->listFollowing($this->Auth->user('id'));
debug($following);
$this->paginate = array('limit'=>20,'conditions'=>array('Post.id'=>array($following['Follower']['post_id']),'Post.status'=>array(1,2)),'order'=>array('Post.datetime'=>'desc'),
'contain'=>array('User','Answer'=>array('User'),'Tag'));
$this->set('posts',$this->paginate());
You will see what is returned from listFollowing() that is causing an index error. You can further debug it with that information. I can no longer help as there is no database schema, full code, the variables/situation that causes this problem, etc :)

Categories