Sunday, February 2, 2014

Getting information from a model inside of a model file

For years I worked with a custom framework that was loosely based on CakePHP.  In that framework, we could access the model data within a model file by using $this->PROPERTY_NAME.  I have seen some Cake examples that use similar code, but for some reason, I can't get it to work.  Here is the function that I'm working with (taken from the InvoiceItem model):

public function getTableRowView(){
pr($this->data);
pr("id: ".$this->id);
pr("quantity: ".$this->data['InvoiceItem']['quantity']);
$html = "<td>$this->id</td>";
$html .="<td>".$this->InvoiceItem['quantity']."</td>";
$html .="<td>Notes</td>";
$html .="<td>Type</td>";
$html .="<td>Subtotal</td>";
return $html;
}

When I print out $this->data, this is the information that I get.  That part seems to be working just fine.

Array  (      [InvoiceItem] => Array          (              [id] => 121              [amount] => 1.00              [quantity] => 1              [notes] => Note 1              [invoice_item_type_id] => 1              [invoice_id] => 1          )        [InvoiceItemType] => Array          (              [id] => 1              [name] => Charge          )        [Invoice] => Array          (              [id] => 1              [name] => Invoice 1          )    )

The $id property can be accessed without any problems.  But the only way that I can get to any of the other information is to go through the data array: $this->data['InvoiceItem']['quantity'].

Is this the only way to get to the data in a model?  Shouldn't I just be able to use $this->quantity?  Thank you in advance for any help you can give me!

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: