I added some item inside cart session(Array) but i want to remove 1 rows for that i wrote following code but that is not working for me.
public function deletecart() {
$this->loadModel("Product");
if($this->Session->check('cart') AND count($this->Session->read('cart'))>0)
{
foreach ($this->Session->read('cart') as $key => $value) {
if ($value['0']['Product']['id'] == "12") {
unset($this->Session->read('cart')[$key]);
}
}
}
}
Here is my session debug value
array(
'[0]' => array(
(int) 0 => array(
'Product' => array(
'id' => '8',
'category' => 'Pendant',
)
)
),
(int) 1 => array(
(int) 0 => array(
'Product' => array(
'id' => '12',
'category' => 'Pendant'
)
)
)
)
You can't unset session key value like this. You will have to store Session key value in a temporary variable.
$sessionArr = $this->Session->read('cart');
foreach ($sessionArr as $key => $value) {
if ($value['0']['Product']['id'] == "12") {
// Unset key
unset($sessionArr[$key]);
}
}
// Assign $sessionArr value to cart key
$this->Session->write('cart',$sessionArr);
Related
Is it possible to retrieve records where the index is the record id?
I have a model with a hasMany Relation. So my study can have multiple texts and questions etc.
The custom retrieve function looks like this:
protected function _findStudyWithSequence($state, $query, $results=array()){
if ($state === 'before') {
$query['contain'] = array(
'SubjectFilter'=>array('fields'=>'SubjectFilter.studyCode'),
'Text'=>array('fields'=>'Text.position,Text.id,Text.text','order'=>array('position')),
'Image'=>array('fields'=>'Image.position, Image.id','order'=>array('position')),
'Video'=>array('fields'=>'Video.position, Video.id','order'=>array('position')),
'Web'=>array('fields'=>'Web.position, Web.id','order'=>array('position')),
'Likert'=>array('fields'=>'Likert.position, Likert.id','order'=>array('position')),
'Question'=>array('fields'=>'Question.position, Question.id','order'=>array('position')),
'Star'=>array('fields'=>'Star.position, Star.id','order'=>array('position')),
'Multiple_choice'=>array('fields'=>'Multiple_choice.position, Multiple_choice.id','order'=>array('position'))
);
$query['recursive']=-1;
$query['order']=array('Study.started DESC');
$query['fields']=array(
'Study.id', 'Study.user_id','Study.title','Study.description','Study.numberParticipants','Study.state','Study.created','Study.modified','Study.started','Study.completed','Study.numberParticipants');
return $query;
}
}
return $results;
}
As you can see I am using containable
The results look like this:
array(
'Study' => array(
'id' => '1845346986',
'user_id' => '1402402538',
'title' => 'bla bla',
'description' => '',
'numberParticipants' => '10',
'state' => 'active',
'created' => '2017-08-21 14:06:11',
'modified' => '2017-08-21 14:29:56',
'started' => '2017-08-21 14:30:06',
'completed' => '0000-00-00 00:00:00',
),
'SubjectFilter' => array(
'studyCode' => null
),
'Text' => array(
(int) 0 => array(
'position' => '0',
'id' => '1423909242',
'text' => '<p>Bla <b>bla </b></p><p>blub</p><p><br /></p>',
'study_id' => '1845346986'
)
),
'Question' => array(
(int) 0 => array(
'position' => '4',
'id' => '729521908',
'study_id' => '1845346986'
)
), etc
But I want the Text and Question index to be the ids. Like this:
'Text' => array(
(int) 1423909242 => array(
'position' => '0',
'id' => '1423909242',
'text' => '<p>Bla <b>bla </b></p><p>blub</p><p><br /></p>',
'study_id' => '1845346986'
)
),
How can i manage this?
You can use CakePHP's amazing Hash::combine() method.
Something like:
$results['Text'] = Hash::combine($results['Text'], '{n}.id', '{n}');
$results['Question'] = Hash::combine($results['Question'], '{n}.id', '{n}');
return $results;
It's not tested code, but I believe it's correct (or at least close), and you should get the idea from here.
Notes on Hash::combine:
Creates an associative array using a $keyPath as the path to build its
keys, and optionally $valuePath as path to get the values. If
$valuePath is not specified, or doesn’t match anything, values will be
initialized to null. You can optionally group the values by what is
obtained when following the path specified in $groupPath.
Hash::combine(array $data, $keyPath, $valuePath = null, $groupPath =
null)
I'm having some trouble trying to paginate related data in a hasMany relationship
I have two models - Collection and Item
Collection hasMany Item
Item belongsTo Collection
In the Collection view.ctp it shows the collection details for the ID being passed along with related data of the Items. It receives this data from a find "first" call.
$this->set('collection', $this->Collection->find('first', $options));
With the resulting array looking like:
array(
'Collection' => array(
'id' => '4fc923f5-0a58-453f-9507-0c0c86106d80',
'name' => '<collection_name>'
),
'Item' => array(
(int) 0 => array(
'id' => '4fc92403-000c-4ed2-9dae-0c0c86106d80',
'name' => 'Item1'
),
(int) 1 => array(
'id' => '4fc9241b-35ec-4810-b511-0c0c86106d80',
'name' => 'Item2'
),
(int) 2 => array(
'id' => '4fc96e5d-ad74-4c05-a9da-0c0c86106d80',
'name' => 'Item3'
),
(int) 3 => array(
'id' => '4fc96e64-a110-4036-bea2-0c0c86106d80',
'name' => 'Item4'
)
)
Now I can get the same results from this paginate call except there is the added 0 index
$this->set('items', $this->paginate('Collection', array('Collection.id'=>$id)));
array(
(int) 0 => array(
'Collection' => array(
'id' => '4fc923f5-0a58-453f-9507-0c0c86106d80',
'name' => '<collection_name>'
),
'Item' => array(
(int) 0 => array(
'id' => '4fc92403-000c-4ed2-9dae-0c0c86106d80',
'name' => 'Item1'
),
(int) 1 => array(
'id' => '4fc9241b-35ec-4810-b511-0c0c86106d80',
'name' => 'Item2'
),
(int) 2 => array(
'id' => '4fc96e5d-ad74-4c05-a9da-0c0c86106d80',
'name' => 'Item3'
),
(int) 3 => array(
'id' => '4fc96e64-a110-4036-bea2-0c0c86106d80',
'name' => 'Item4'
)
)
)
);
I can perform an array_shift on the array returned but then the pagination doesn't work. I can try creating a custom paginate function for this but wanted to know if there's a simplier way to do this with the standard paginate I'm using since it has the data I want, just extra first array parent element [0].
Thanks in advance for any suggestions.
<?php
// Don't pull in child data, we'll fetch that manually.
$this->Collection->contain(); // Assuming you have this behavior.
// Get the collection.
$collection = $this->Collection->find('first', $options);
// Conditions
$this->paginate['Item'] = array(
'conditions' => array(
'Item.collection_id' => $collection['Collection']['id'];
)
);
// Get the items
$items = $this->paginate('Item');
// Return to original child model structure.
array_walk($items, function(&$v) { // Anonymous functions requires PHP 5.3
$v = $v['Item'];
});
// Add it to the collection array.
$collection['Item'] = $items;
?>
That's the way I implement pagination on child models. Give it a shot, might work for you too.
-A
How can I keep the same checkboxes checked after submit? All the other input fields on the form automatically keeps the values. I thought this would also go for checkboxes, but nope.
echo $this->Form->input('type_id', array(
'multiple' => 'checkbox',
'options' => array(
'1' => 'Til salgs',
'2' => 'Ønskes kjøpt',
'3' => 'Gis bort'
),
'div' => false,
'label' => false
));
I believe this can be done in the controller, but how?
Edit:
Since I posted this question I've changed to CakeDcs Search plugin, because I've gotten this to work with that before. Still... I can't get it to work this time.
Adding model and controller code:
AppController
public $components = array('DebugKit.Toolbar',
'Session',
'Auth' => array(
'loginAction' => '/',
'loginRedirect' => '/login',
'logoutRedirect' => '/',
'authError' => 'Du må logge inn for å vise denne siden.',
'authorize' => array('Controller'),
),
'Search.Prg'
);
public $presetVars = true; //Same as in model filterArgs(). For Search-plugin.
AdsController
public function view() {
$this->set('title_for_layout', 'Localtrade Norway');
$this->set('show_searchbar', true); //Shows searchbar div in view
$this->log($this->request->data, 'debug');
//Setting users home commune as default filter when the form is not submitted.
$default_filter = array(
'Ad.commune_id' => $this->Auth->user('User.commune_id')
);
$this->Prg->commonProcess(); //Search-plugin
$this->paginate = array(
'conditions' => array_merge($default_filter, $this->Ad->parseCriteria($this->passedArgs)), //If Ad.commune_id is empty in second array, then the first will be used.
'fields' => $this->Ad->setFields(),
'limit' => 3
);
$this->set('res', $this->paginate());
}
Model
public $actsAs = array('Search.Searchable');
public $filterArgs = array(
'search_field' => array('type' => 'query', 'method' => 'filterSearchField'),
'commune_id' => array('type' => 'value'),
'type_id' => array('type' => 'int')
);
public function filterSearchField($data) {
if (empty($data['search_field'])) {
return array();
}
$str_filter = '%' . $data['search_field'] . '%';
return array(
'OR' => array(
$this->alias . '.title LIKE' => $str_filter,
$this->alias . '.description LIKE' => $str_filter,
)
);
}
/**
* Sets the fields which will be returned by the search.
*
* #access public
* #return array Database table fields
* #author Morten Flydahl
*
*/
public function setFields() {
return array(
'Ad.id',
'Ad.title',
'Ad.description',
'Ad.price',
'Ad.modified',
'User.id',
'User.first_name',
'User.middle_name',
'User.last_name',
'User.link',
'User.picture_url',
'Commune.name',
'Type.id',
'Type.name'
);
}
You have to set manually the selected option of the input, as an array with "keys = values = intval(checkbox id)"
I cannot explain why this format, but this is the only way I get it to work.
Here is my code:
echo $this->Form->create('User');
// Read the submitted value
$selected = $this->Form->value('User.Albums');
// Formats the value
if (empty($selected)) {
$selected = array(); // avoid mess
} else {
$selected = array_map('intval', $selected);
$selected = array_combine ($selected, $selected);
}
// Renders the checkboxes
echo $this->Form->input('Albums',array(
'type' => 'select',
'multiple' => 'checkbox',
'options' => $albums, // array ( (int)id => string(label), ... )
'selected' => $selected, // array ( (int)id => (int)id, ... )
));
Hope this helps.
++
I am struggeling with a custom find in cakephp 2.1.
In my model I have this function:
public function findByGenres($data = array()) {
$this->Item2genre->Behaviors->attach('Containable', array('autoFields' => false));
$this->Item2genre->Behaviors->attach('Search.Searchable');
$query = $this->Item2genre->getQuery('all', array(
'conditions' => array('Genre.name' => $data['genre']),
'fields' => array('item_id'),
'contain' => array('Genre')
));
return $query;
}
This returns the following query:
SELECT `Item`.`id` FROM `items` AS `Item`
WHERE `Item`.`id`
IN(SELECT `Item2genre`.`item_id` FROM `item2genre` AS Item2genre
LEFT JOIN `genres` AS Genre ON(`genre_id` = `Genre`.`id`)
WHERE `Genre`.`name`
IN ('Comedy', 'Thriller')
)
The result of the query returns Items with either 'Comedy' or 'Thriller' genre associated to them.
How can I modify the query to only return Items with 'Comedy' AND 'Thriller' genre associated to them?
Any suggestions?
edit:
content of data is:
'genre' => array(
(int) 0 => 'Comedy',
(int) 1 => 'Thriller'
)
You would want your 'conditions' key to be this:
'conditions' => array(
array('Genre.name' => 'Comedy'),
array('Genre.name' => 'Thriller')
)
So specifically to your problem your $data['genre'] is array('Comedy', 'Thriller'). So you could create a variable that has contents similar to what you need by doing:
$conditions = array();
foreach ($data['genre'] as $genre) {
$conditions[] = array('Genre.name' => $genre);
}
To get the DB result in basis of condition
if($keyword!='')
build condition;
/*
array('conditions' => array("AND" => array ("esl.esl_artistname LIKE"=>"%".$artistname."%",
"esl.esl_songname LIKE"=>"%".$songname."%")),
'limit' => $arrFind['limit'],
'page'=>$arrFind['page']));
*/
if(!name!='')
/*
array('conditions' => array("esl.esl_artistname LIKE"=>"%".$artistname."%"),
'limit' => $arrFind['limit'],
'page'=>$arrFind['page'] ))
*/
$this->find('all',condition);
how to do this? how to concatenate both conditions?
Why not initialize the conditions array and just append to it?
$conditions = array();
if( keyword != '' ) {
array_push(
'conditions'
, array( "AND" => array ("esl.esl_artistname LIKE"=>"%".$artistname."%", "esl.esl_songname LIKE"=>"%".$songname."%" ) )
}
if( !name != '' ) {
array_push( 'conditions', array("esl.esl_artistname LIKE"=>"%".$artistname."%")
}
$this->find( 'all', array( 'conditions' => $conditions, 'limit' => $arrFind['limit'], 'page' => $arrFind['page'];
I would do something like Rob Wilkerson suggested but like this:
$conditions = array();
if(keyword != '') {
$conditions[] = array( "AND" => array ("esl.esl_artistname LIKE"=>"%".$artistname."%", "esl.esl_songname LIKE"=>"%".$songname."%" ) );
}
if(!name != '') {
$conditions[] = array("esl.esl_artistname LIKE"=>"%".$artistname."%");
}
$this->find( 'all', array( 'conditions' => $conditions, 'limit' => $arrFind['limit'], 'page' => $arrFind['page'];
Concatenating condition could be done in following way:
$condition1 = array('Model1.field1 LIKE'=>'%'.$value.'%', 'Model1.field2'=>2);
$condition2 = array('Model2.field2 LIKE'=>'%'.$value.'%', 'Model2.field2'=>1);
$this->MyModel->find('all', array('conditions'=>am($condition1, $condition2)));
am is the cake's shortcut to array_merge.