Here I am converting $allProductData to array because there I have to apply foreach loop and some conditions after that I am assigning $allProductData to pagination but I am getting error "Unable to locate an object compatible with paginate." so how can I convert array to Cake\ORM\Query object to pass into pagination?
$this->PurchaseRequisitionProducts->hasMany('PurchaseOrderProducts', [
'bindingKey'=>'product_id',
"foreignKey"=>'product_id'
]);
$allProductData = $this->PurchaseRequisitionProducts->find('all',[
'contain' => ['PurchaseOrderProducts'=>function($c){
return $c->where(['PurchaseOrderProducts.id IS NOT NULL']);
},'PurchaseOrderProducts.PurchaseOrder'=>function($p){
return $p->where(['PurchaseOrder.id IS NOT NULL'])
->where(['PurchaseOrder.is_approve'=>"Y"])
->where(['PurchaseOrder.po_type'=>1])
->where(['PurchaseOrder.status'=>1]);
},'PurchaseOrderProducts.PurchaseOrder.CompanyMaster','PurchaseRequisition.Users.EmployeeDetails.Departments','ProductCategory','ProductSubcategory','Currency','PurchaseRequisition','ProductsMaster','Uom'] ,
'conditions'=>[$condn,$conditions],
//"sortWhitelist"=>["id",'EmployeeDetails.first_name','Departments.department',"pr_reference_no","pr_date",'purchase_requisition_id', "ProductsMaster.product_name","ref_price","qty","approved_qty"],
'order'=>["PurchaseRequisitionProducts.id"=>"desc"],
])->toArray();
$pr_product = $this->paginate($allData)->toArray();
if($pr_product){
foreach($pr_product as $key1=>$prProd){
if(empty($prProd['purchase_order_products']) || !isset($prProd['purchase_order_products']) || $prProd['purchase_order_products']==null || $prProd['purchase_order_products']=='' || empty($prProd['purchase_order_products'])){
unset($pr_product[$key1]);
}
if(isset($prProd['purchase_order_products'])){
$supplier=[];
$poarray=[];
foreach($prProd['purchase_order_products'] as $key2=>$poProd){
if($poProd['purchase_order']==null || $poProd['purchase_order']=='' || empty($poProd['purchase_order'])){
unset($prProd['purchase_order_products'][$key2]);
}
$supplier[]=$poProd['purchase_order']['supplier_id'];
//debug($supplier);
$companies= $this->CompanyMaster->find('list', [
'keyField' => 'id','valueField' => 'Company_name',
'conditions'=>['id IN'=>$supplier],
])->toArray();
$pr_product[$key1]['supplier']=$companies;
}
if(empty($prProd['supplier'])){
unset($pr_product[$key1]);
}
}
}
}
You can do it this way also, you have to remove toArray() in order to make pagination work.
$allProductData = $this->PurchaseRequisitionProducts->find('all')->contain(['PurchaseOrderProducts']);
$pr_product = $this->paginate($allProductData);
Also, define this public variable
public $paginate = [
'limit' => 25,
'order' => [
'TableName.columnName' => 'asc' //not mandatory
]
];
Instead of adding to array in find all query you should add it on the result of pagination like:
$allProductData = $this->PurchaseRequisitionProducts->find('all',[
'contain' => ['PurchaseOrderProducts']
]);// remove toArray() from here
$pr_product = $this->paginate($allProductData)->toArray(); // AddtoArray() here
OR you can try like this:
$paginate = [
'contain' => ['PurchaseOrderProducts'],
'limit' => 10
];
$this->set('data', $this->Paginator->paginate($this->PurchaseRequisitionProducts->find('all'), $paginate)->toArray());
Related
Controller Code:
public $paginate = [
'fields' => ['Subscribes.id'],
'limit' => 1,
'order' => [
'Subscribes.created' => 'desc'
]
];
Controller Method Code: Here $type is variable, value can be interger 1,2 3 etc
$customFinderOptions = [
'type' => $type
];
$this->paginate = [
'finder' => [
'typed' => $customFinderOptions
]
];
$contacts = $this->paginate($this->Subscribes);
Model Code:
public function findTyped(Query $query, array $options) {
$type = $options['type'];
return $query->where(['type' => $options['type']]);
}
Error: As you can see limit is set to 1 but it returns all the records. Which means pagination is not working with custom finder method.
It doesn't work, because you are overwriting your $paginate variable. Set it once in your controller action, use array_merge(), or update just single key:
$this->paginate["finder"] = [...];
Hi folks! I'm trying to transfer data as array from the controller to the model, and then paste the data into the query builder, but the data must be in the same order as specified in the columns.
What options do I have?
And do you think this is a bad practice?
Controller:
$responseNotes = Model::factory('Notes')-> createTicket([
'description' => htmlspecialchars($_POST['description']),
'contact_id' => $_POST['contact_id'],
'pref_contact' => $_POST['pref_contact'],
'dog_id' => $_POST['document_id'],
'type' => $_POST['type'],
'owner_id' => Auth::instance()->get_user()->id,
'cc' => $_POST['cc-emails'],
'title' => $_POST['title']
]);
Model:
public function createNote(array $data)
{
$columns = [
'type',
'owner_id',
'cc',
'title',
'description',
'contact_id',
'pref_contact',
'dog_id'
];
if (!array_diff($columns, array_keys($data))) {
// All needed values exists
$result = DB::insert($this->NOTES, $columns)-> values($data)-> execute($this->SCHEMA);
}
return ($result) ? $result : false ;
}
Thanks to this answer. Solved this by:
// Order $data array according to $column.values
$orderedData = [];
foreach ($columns as $key) {
$orderedData[$key] = $data[$key];
}
$result = DB::insert($this->TICKETS, $columns)
-> values($orderedData)
-> execute($this->SCHEMA);
Why you don't use ORM Model?
in controller:
$responseNotes = ORM::factory('Notes')-> values([
'description' => htmlspecialchars($_POST['description']),
'contact_id' => $_POST['contact_id'],
'pref_contact' => $_POST['pref_contact'],
'dog_id' => $_POST['document_id'],
'type' => $_POST['type'],
'owner_id' => Auth::instance()->get_user()->id,
'cc' => $_POST['cc-emails'],
'title' => $_POST['title']
])
try{
$responseNotes->save();
} catch (ORM_Validation_Exception $ex) {
print_r($ex->errors('models'));
}
And don't use htmlspecialchars($_POST['description'])
In model class modify function (doc):
public function filters()
{
return array(
'description' => array( array('htmlspecialchars') ),
);
}
It looks like You have associative array with structure db_column=>value right? Than You can simply insert like this:
DB::Insert('table_name',array_keys($data))->values(array_values($data))->execute();
I'm trying to update the status on my tickets table to the value : 2.
Once I can create the comment... (is working.. :) ), I wanted to change the status to 2.
This is my ticket model and the following function:
public function addComment($id,$body,$solved)
{
$this->find($id)->status = 2;
$this->save();
$this->comments()->create([
'ticket_id' => $id,
'body' => $body,
'user_id' => auth()->id()
]);
}
You need to get your object first then you can Update it:
public function addComment($id,$body,$solved)
{
$ticket = $this->find($id);
$ticket->status = 2;
$ticket->save();
$ticket->comments()->create([
'ticket_id' => $id,
'body' => $body,
'user_id' => auth()->id()
]);
}
Try changing your code like this. Maybe this will fix the problem you're having:
public function addComment($id,$body,$solved)
{
$ticket = Ticket::find($id);
$ticket->status = 2;
$ticket->save();
$ticket->comments()->create([
'ticket_id' => $id,
'body' => $body,
'user_id' => auth()->id()
]);
}
I am paginating associated model in view() function of another model.
public function view($id = null)
{
$category = $this->Categories->get($id, [
'contain' => ['Subcategories']
]);
// paginate products
$products = $this->paginate($this->Categories->Products->findByCategoryId($category->id, ['conditions' => ['stock >' => 0, 'selling_price >' => 0]]), [
'limit' => 21
]);
$this->set('products', $products);
$this->set('category', $category);
$this->set('_serialize', ['category']);
}
I want to restrict data to find where stock and selling_price is greater than 0.
But this is not working. How to apply conditions on findById ?
Try this :
$products = $this->paginate($this->Products->findByCategoryId($categoryId)->where(['stock >' => 0, 'selling_price >' => 0])->limit(21));
with $categoryId given.
I am creating a custom datasource and I am having problems when i request find('list'). find('all') returns perfectly what I want within my controller but find('list') just returns an empty array.
The funny thing is if I do a die(Debug($results)) in the datasource within the read function then I get my find('list') array correctly but if I return it i then get an empty array in my controller. Any ideas?
Code below:
public function read(Model $model, $queryData = array(), $recursive = null) {
if ($queryData['fields'] == 'COUNT') {
return array(array(array('count' => 1)));
}
$this->modelAlias = $model->alias;
$this->suffix = str_replace('Flexipay', '', $model->alias);
if(empty($model->id)){
$this->url = sprintf('%s%s%s', $this->sourceUrl, 'getAll', Inflector::pluralize($this->suffix));
}
$r = $this->Http->get($this->url, $this->config);
if($r->isOk()){
$results_src = json_decode($r->body, true);
if(is_array($results_src)){
//$this->find('list');
if($model->findQueryType == 'list'){
return $this->findList($queryData, $recursive, $results_src);
}
//$this->find('all');
foreach($results_src['PortalMandantenResponses']['portalMandantenResponses'] as $r){
$results[] = $r;
}
if(!empty($results)){
$e = array($model->alias => $results);
return $e;
}
}
}else{
//
}
return false;
}
My response from die(debug(array($model->alias => $results);
(int) 0 => array(
'Mandant' => array(
'ns2.id' => (int) 79129,
'ns2.name' => 'company a'
)
),
(int) 1 => array(
'Mandant' => array(
'ns2.id' => (int) 70000,
'ns2.name' => 'company b'
)
),
Controller Code is here:
public function test2(){
//$a = $this->User->find('list');
//die(debug($a));
$this->loadModel('Pay.Mandant');
$a = $this->Mandant->find('list', array('fields' => array('ns2.systembenutzernr', 'ns2.systembenutzernrBezeichnung')));
die(debug($a));
}
use,
$a = $this->Mandant->find('list', array('fields' => array('ns2.systembenutzernr', 'ns2.systembenutzernrBezeichnung')));
$this->set(compact('a'));
You can use $a for the dropdown creation in view file.
I just had the same problem writing my custom model though I don't know if the cause in your case is the same, though you should probably look in the same place.
in Model.php there is a function _findList($state, $query, $results), my issue was the fields you specify in the find() call must match the $results structure exactly, otherwise at the end of the _findList() function the call to:
Hash::combine($results, $query['list']['keyPath'], $query['list']['valuePath'], $query['list']['groupPath'])
returns the empty array. The keyPath of {n}.MODELNAME.id, etc must match the name of the model specified in $results, for example
[0] => ['MODELNAME'] = array()
[1] => ['MODELNAME'] = array()
In my case my keyPath and valuePath had a different value for MODELNAME than in the results array
Hope that helps