I use the plugin cakedc search, but there is one thing I can not seem to find, I have a form with two fields, and I would add for example "online = 1" to my search
in my controller, I trying:
public function find() {
$this->Prg->commonProcess();
$array = array('type'=>'post', 'online'=>'1', 'created'=> '<= NOW()');
$this->passedArgs = Set::merge($this->passedArgs, $array);
$this->paginate = array(
'conditions' => array($this->Post->parseCriteria($this->passedArgs)),
);
$this->set('posts', $this->paginate());
model:
public $displayField = array('name', 'category_id');
public $filterArgs = array(
array('name' => 'name', 'type' => 'query', 'method' => 'filterName'),
array('name' => 'category_id', 'type' => 'value')
);
.....
debug($this->passedArgs):
array(
'name' => 'mon',
'category_id' => '2',
'type' => 'post',
'online' => '1',
'created' => '<= NOW()'
)
but $array argument are not taken in my search
Can anyone help?
I am a beginner with cakephp,
Thanks a lot !
Instead of
'conditions' => array($this->Post->parseCriteria($this->passedArgs)),
try
'conditions' => $this->Post->parseCriteria($this->passedArgs),
I also recommend you to use DebugKit and to add the generated query to your question in the case it is still not working.
Related
I've got the following models:
Run->hasMany ActualResult
ActualResult belongs to Status
When I view an ActualResult, Cake gets the corresponding Status without straight out of the box.
When I view a Run, I want to paginate the ActualResults. I have managed to get this to almost work with the following code in RunsController::view():
public function view($id = null) {
if (!$this->Run->exists($id)) {
throw new NotFoundException(__('Invalid run'));
}
$this->Run->contain ( 'Pack', 'User', 'Status');
$options = array('conditions' => array('Run.' . $this->Run->primaryKey => $id));
$run = $this->Run->find('first', $options);
$this->set('run', $run);
// Paginate the ActualResults
$this->paginate = array(
'contain' => array('Status'),
'order' => 'ActualResult.format',
'limit'=>5 ,
'conditions' => array('ActualResult.run_id' => $id)
);
$actualResults = $this->Paginator->paginate('ActualResult');
$this->set('actualResults',$actualResults);
}
The problem is that I get a warning:
Warning (512): Model "ActualResult" is not associated with model "Status" [CORE\Cake\Model\Behavior\ContainableBehavior.php, line 343
Something v. similar works for me in another model association, and as mentioned view() in ActualResultController works fine, so I am stumped.
Can anyone help?
Here are the model assocations:
In Run.php:
public $hasMany = array(
'ActualResult' => array(
'className' => 'actual_result',
'foreignKey' => 'run_id',
'dependent' => true,
'finderQuery' => 'SELECT ActualResult.*, Txn.name, Status.name FROM actual_results AS ActualResult, txns as Txn, codes AS Status WHERE ActualResult.run_id = {$__cakeID__$} and Txn.id = ActualResult.txn_id and (Status.code = ActualResult.status and Status.code_type = "ARS");'
)
);
In ActualResult.php
public $belongsTo = array(
'Run' => array(
'className' => 'Run',
'foreignKey' => 'run_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Status' => array(
'class`enter code here`Name' => 'Code',
'foreignKey' => 'status',
'conditions' => 'code_type = "ARS"',
'fields' => '',
'order' => ''
)
);
I have the following Code to get a special model related to another model related to actual object: (I want the Entry-model - in the Rubric-model-view)
$this->paginate = array(
'Entrieslocation' => array(
'conditions' => array('Entrieslocation.rubric_id' => $this->Rubric->id, 'Entrieslocation.subrubric_id' => '0'),
'joins' => array(
array(
'alias' => 'Entry',
'table' => 'entries',
'type' => 'INNER',
'conditions' => 'Entry.id = Entrieslocation.entry_id'
)
)
)
);
Then I thought I could get it by something like this:
$this->set('entries', $this->paginate('Entry'));
But the variable is empty, how can I access the Entries inside the Pagination in the view?
The Query generated by the code above is correct, I checked it by entering the query into phpmyadmin. So how can I access the result (only the Entries)?
Thanks for your help :)
First of all you must define $paginate variable below controller class. For example
class UsersController extends AppController {
public $paginate = array();
}
Then in your action follow following patter to get the paginated result.
$this->paginate = array(
'recursive' => -1,
'limit' => 10,
'conditions' => array('Your conditions'),
'fields' => array('Entry.*, Entrieslocations.*'),
'joins' => array(
array(
'table' => 'Entrieslocations',
'alias' => 'Entrieslocation',
'type' => 'inner',
'conditions' => 'Entry.id = Entrieslocation.entry_id'
),
),
'order' => array(
'Entry.id' => 'desc'
)
);
$entries = $this->paginate('Entry');
To pass result set to the view use following:
$this->set('entries', $entries);
My pagination worked perfectly like this :
var $paginate = array(
'Article' => array(
'conditions' => array(
'Article.visible' => true),
'order' => array('Article.creation_date DESC', 'Article.id DESC'),
'limit' => 11
)
);
But I want to filter my articles with a publication date like this
var $paginate = array(
'Article' => array(
'conditions' => array(
'Article.visible' => true,
'Article.publication_date <= ' => date('Y-m-d H:i:s')),
'order' => array('Article.creation_date DESC', 'Article.id DESC'),
'limit' => 11
)
);
But the date() function does not seem to be accepted.
It's not the good syntax.
Does anyone can help me?
Thanks a lot in advance.
If you want to do this, you will have to use $this->paginate = array() either in the action that you are paginating in, or in the __construct() function.
You cannot execute functions like that when the object is instantiated.
This is true for any type of method call at runtime.
You can't use a function in the declaration of an array at the class level. What you could do is to use a beforeFilter() callback in your controller to initialize the $paginate variable like this:
function beforeFilter()
{
parent :: beforeFilter();
$this->paginate = array(
'Article' => array(
'conditions' => array(
'Article.visible' => true,
'Article.publication_date <=' => date('Y-m-d H:i:s')),
'order' => array('Article.creation_date DESC', 'Article.id DESC'),
'limit' => 11
)
);
}
I have this function in CakePHP's AppModel (This is used to install initial data in CakePHP) However, I can't seem to get it to save my data, and I get no errors.
Here is my function inside App/Model/AppModel.php:
public function importData() {
$initialOptionData = array(
array( 'Option' => array( 'name' => 'version', 'value' => '1.0.0', )),
array( 'Option' => array( 'name' => 'site-name', 'value' => 'Site Title', )),
);
$this->create();
$this->saveMany($initialOptionData);
}
From you posted code it seems you're trying to save you data to options table, and to do that you need to use Option model.
But you're code is within AppModel, so first import Option model and then execute your save statements.
Your code should look like:
public function importData() {
$initialOptionData = array(
array( 'Option' => array( 'name' => 'version', 'value' => '1.0.0', )),
array( 'Option' => array( 'name' => 'site-name', 'value' => 'Site Title', )),
);
App::import('model','Option'); // Import the Option Model
$Option = new Option(); // create instance of Option class
// save statements
$Option->create();
$Option->saveMany($initialOptionData);
}
Note
Code you're trying will work if you write that within app/model/Option.php file.
I suppose it would be enough to store the values as model property. No need to call $this->save...
public $initialOptionData = array(
array( 'Option' => array( 'name' => 'version', 'value' => '1.0.0', )),
array( 'Option' => array( 'name' => 'site-name', 'value' => 'Site Title', )),
);
all models will inherit initialOptionData...
I have several models that all share one fields (say name) but differ in other fields. Is there any wan to find all of ModelX ModelY and ModelZ that have name 'foo'. Thanks for any suggestions.
Something like this ?
$models = array('Post', 'Comment', 'Article');
$results = array();
foreach($models as $model) {
$this->loadModel($model);
$results[$model] = $this->{$model}->find('all', array(
'conditions' => array(
$model'.name' => $name
)
));
}
If the they are related you should be able to specify the Model followed by '.' within the find.
$this->find('all', array(
'conditions' => array(
'OR' => array(
'Article.field' => 'foo',
'Comment.field' => 'foo',
'Post.field' => 'foo',
)
),
'fields' => array('Article.field', 'Comment.field', 'Post.field')
));