php - CakePHP Posting Error

131

Issue: Getting the following error in my controller function. The initial save is working, it looks like it's getting choked up on the nested save function. Thoughts as to why i would be getting this error?

Error: Fatal Error Error: Call to a member function read() on a non-object File: C:\metroapps\Orders\app\Controller\OrderLogsController.php
Line: 121

Notice: If you want to customize this error message, create app\View\Errors\fatal_error.ctp

Code:

    public function quickAdd($notes = null,$order_id =null, $log_type_id = null) {
            $this->request->data['OrderLog']['log_type_id'] = $log_type_id;
            $this->request->data['OrderLog']['order_id'] = $order_id;
            $this->request->data['OrderLog']['notes'] = $notes;
            $this->OrderLog->create();
    if ($this->OrderLog->save($this->request->data)) {
                $this->Session->setFlash(__('The order log has been saved.'));
                $this->Order->read(null, $order_id);
                //set checkedout to false
                $this->Order->set('checked_out', 0);
                $this->Order->set('checked_out_by', null);
                $this->Order->save();
                return $this->redirect(array('controller' => 'orders','action' => 'view',$order_id));
    } else {
                $this->Session->setFlash(__('The order log could not be saved. Please, try again.'));
    }

    $orders = $this->OrderLog->Order->find('list');
    $logTypes = $this->OrderLog->LogType->find('list');
    $this->set(compact('orders', 'logTypes'));
}
507

Answer

Solution:

You probably need to fix your model relations and access the related model properly.

Make sure your Order and OrderLog models are related (both directions)

// in Order model
public $hasMany = array('OrderLog');

// in OrderLog model
public $belongsTo = array('Order');

and then in your function do

$this->OrderLog->Order-> ..

// instead of $this->Order

People are also looking for solutions to the problem: How do I pass form data to PHP and return a json string to Javascript?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.