PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, February 21, 2022

[FIXED] Invoking cells directly from the controller

 February 21, 2022     cakephp, cakephp-3.0     No comments   

Issue

I am working with cakephp 3.1.7 and figuring out how to invoke view cells or retrieve cell data from the controller. I implemented basic cells with the help of cakephp docs and also http://josediazgonzalez.com/2014/03/20/view-cells/ document which is working fine. However, when I try to return cells directly from the controller I get the following error.

Error: Call to undefined method App\Controller\ProductsController::decorate()

This is what I have:

use Cake\View\Cell;
use Cake\ORM\TableRegistry;
class ProductupdateCell extends Cell
{               
    public function display($options = []){ 

            if (!empty($options['displaylist'])) {
                $this->set('productlist', $options['displaylist']);
                return $this; 
            }else{  
                $category = $this->request->query['category'];              
                $this->loadModel('Products');
                $query   =  $this->Products-> find() 
                                           -> where(['Products.category' => $category])                                             
                                           -> hydrate(false);
                $productlist = $query->toArray();
                $this->set('productlist',$productlist);
                return $this; 
            }       
    }
}

In my controller,

<?php
class ProductsController extends Controller
{
use CellTrait;
public function view($id)
{
    $products = $this->Products->findById($id);        
    $this->set('displaylist', $this->decorate('ProductupdateCell', $products));
}
}

Please correct me where I am going wrong. Is it efficient to use this to update my product list based on user input with ajax request? Can I selectively update the particular cell rendered in my view page? Is there any other method to update the cell directly. Please forgive me if this is a dumb question.


Solution

I am working with cakephp 3.1.7 and figuring out how to invoke view cells or retrieve cell data from the controller.

This is an architecturally wrong. They're supposed to be used from the view level.

If you want to have modular and abstracted controller logic either use the CRUD plugin. Or simply go for components. Components are packages of logic that are shared between controllers.

Error: Call to undefined method App\Controller\ProductsController::decorate()

There is no such method in the Controller, CellTrait nor the View class. I don't know from where you got that code, it's also not in the documentation of the cells.



Answered By - floriank
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing