Form for deep association with CakePHP, almost working - forms

I'm writing my first Cake app and trying to set up my first deep association. It's very nearly working but I have a couple of small issues.
The first part of my app is a customer database. A customer has many addresses, and an address has many contacts. The data structure returned contains duplicated data from the address model.
Here's the 3 models:
class Customer extends AppModel {
public $hasMany = 'CustomerAddress';
}
class CustomerAddress extends AppModel {
public $belongsTo = 'Customer';
public $hasMany = 'CustomerContact';
}
class CustomerContact extends AppModel {
public $belongsTo = 'CustomerAddress';
}
In my customer controller, I want to get associated addresses and their contacts. I can do this by setting recursion on the find command:
CustomerController.php
public function find( $id = NULL) {
$this->set('customers', $this->Customer->find('all', array( 'recursive' => 2)));
}
And this works brilliantly, with one caveat. The data structure returned looks like this:
array(
'Customer' => array(
'id' => '46',
....
),
'CustomerAddress' => array(
(int) 0 => array(
'id' => '35',
'customer_id' => '46',
.....
'Customer' => array(
'id' => '46',
.....
),
'CustomerContact' => array(
(int) 0 => array(
'id' => '29',
'customer_address_id' => '35',
.....
)
)
)
)
)
At a glance, this looks fine, and for all intents and purposes works as the data is formatted how you'd expect. But, CustomerAddress also contains a Customer object, which is a duplicate of the top level Customer object. I assume this is because recursion is working with the belongsTo on the address model.
I've tried setting the recursion to 1, but then I only get the address not the contact. I've tried setting the recursion in the address model, but that doesn't seem to effect the find at the Customer level.
It's not a huge issue, just worried about future performance issues and unnecessary calls to the database.

This is one of the reasons why it is discouraged to use the recursive setting. Most turn off recursion (set it to -1) then use the Containable behavior to get associated models instead. Make sure in AppModel, there is the line
public $actsAs = array('Containable');
Then, to get your data:
$this->Customer->find('all', array('contain' => array('CustomerAddress' => array('CustomerContact'))));

Related

Symfony2 - Functional Testing File uploads with dynamically created fields

I'm fighting with functional testing of files uploading.
I will try to simplify my situation. Let's say I have
a company entity, which has 3 fields.
Company {
protected name;
protected tags;
protected images;
}
Images is array of CompanyImage entities, which serve for
storing image files and tags contains array of Tag entities,
which can be m:n connected with companies.
In the form I use jquery, for adding tags and images dynamically.
(you can create images and add them to the company similar to the
collection type symfony tutorial)
Because images and tag arrays are created with jquery, I cannot
simply use something like the turorial line below in the functional test of the company form.
$form['images'][0]->upload('/path/to/image.jpg');
For the setting
of the form values I use simple a little trick described by sstok here
(https://github.com/symfony/symfony/issues/4124)
public function testCompanyCreation() {
...
//option1
$image = new UploadedFile(
'/path/to/image.jpg',
'image.jpg',
'image/jpeg',
123
);
//or option2
//$image = array('tmp_name' => '/path/to/image.jpg', 'name' => 'image.jpg', 'type' => 'image/jpeg', 'size' => 300, 'error' => UPLOAD_ERR_OK);
$companyFormNode = $companyCrawler->selectButton('Create');
$companyForm = $companyFormNode->form();
$values = array(
'company' => array(
'_token' => $companyForm['company[_token]']->getValue(),
'name' => 'test company',
'tags' => array('1'),
'images' => array('0' => (array('file' =>$image))),
),
);
$companySubmitCrawler = $client->request($companyForm->getMethod(), $companyForm->getUri(), $values, $companyForm->getPhpFiles());
}
this works perfectly until I try to upload the image file.
With the option1 I get following exception
Exception: Serialization of 'Symfony\Component\HttpFoundation\File\UploadedFile' is not allowed
when I use option2 I get this
Argument 1 passed to Acme\myBundle\Entity\CompanyImage::setFile() must be an instance of Symfony\Component\HttpFoundation\File\UploadedFile, array given, called in ...\PropertyAccess\PropertyAccessor.php on line 347 and defined (500 Internal Server Error)
I would also like to point out, that the whole form and uploading of the files works without any problems in the browser. I also tried to make the entities serializable, and it didn't help. Do I have a bug somewhere?
I have figured it out (took couple of hours). Files have to be uploaded in a separate array.
$companyForm = $companyFormNode->form();
$values = array(
'company' => array(
'_token' => $companyForm['company[_token]']->getValue(),
'name' => 'test company',
'tags' => array('1')
),
);
$files = array(
'company' => array('images' => array('0' => (array('file' => $image))))
);
$companySubmitCrawler = $client->request(
$companyForm->getMethod(),
$companyForm->getUri(),
$values,
$files
);

FuelPHP ORM Primary Key on model cannot be changed

I've been banging my head with this ORM error:
Fuel\Core\FuelException [ Error ]: Primary key on model Model_CustomValue cannot be changed.
Here are relevant info from my models I'm having issues with:
<?php
use Orm\Model;
class Model_Purchase extends Model
{
protected static $_has_many = array(
'customvalues' => array(
'model_to' => 'Model_CustomValue',
'key_to' => 'purchase_id',
'cascade_delete' => true,
)
);
protected static $_properties = array(
'id',
'customer_id',
'payment_id',
'audit_id',
'created_at',
'updated_at',
);
<?php
use Orm\Model;
class Model_CustomValue extends Model
{
protected static $_table_name = 'customvalues';
protected static $_primary_key = array('purchase_id', 'customfield_id');
protected static $_belongs_to = array(
'purchase' => array(
'key_from' => 'purchase_id',
'model_to' => 'Model_Purchase',
'key_to' => 'id',
),
);
When trying to save the Model_Purchase with an array of Model_CustomValue objects as a property named 'customvalues' on the $purchase object, I get the "Primary key on model Model_CustomValue cannot be changed."
I've tried swapping the key_from/to in the "belongs_to" on the Model_CustomValue, but to no avail.
I'm using Fuel 1.6 (hash: 6e6d764)
Please let me know if more information would be helpful, and I'll provide.
From the FuelPHP forum thread, Harro answered:
You can not have a column which is at the same time FK and PK. Which
you have on your Model_CustomValue.
The reason for that is that when you disconnect a relation, the FK
will be set to NULL, which should not happen with a PK.
I then clarified, for those of us who may need specific examples from the original example, I confirmed the following:
So just re-stating why that's not allowed:
Model_CustomValue uses the "purchase_id" as part of its PK as well as the FK to Model_Purchase. And if the two Models were to be unlinked, that would lead to a null portion of the PK for Model_CustomValue -- which obviously isn't allowed.

Cakephp automatically filled form select for two word named belongTo model

What is right naming or what am I missing to get automagic run for two word named Model. Actual model belong to the two words named model.
Exact example:
Tour belongs to Accommodation type.
in database there is table tours and table accommodation_types
foreign key from tours is tours.accommodation_type_id
Snapshots of code below.
ToursController.php
public function add() {
//...
$accommodation_types = $this->Tour->AccommodationType->find('list');
//...
$this->set(compact('accommodation_types', ...));
}
Tour.php
//...
public $belongsTo = array(
//...
'AccommodationType' => array(
'className' => 'AccommodationType',
'foreignKey' => 'accommodation_type_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
//...
);
Tours/add.ctp (inside a form)
echo $this->Form->input('accommodation_type_id', array('label' => 'Accommodation type'));
As per convention the view vars names should be camelBacked. So rename the view var from $accommodation_types to $accommodationTypes. If you don't follow convention you have to explicitly specify the options var to use like this:
echo $this->Form->input('accommodation_type_id', array('options' => $accommodation_types, 'label' => 'Accommodation type'));

cake php habtm select does not set selected values

Hello I have several troubles with HABTM and Form.
I am using CakePHP 2.3 and PHP 5.3.3
This is my code situation for the understanding of the scenario: Albums and Singles related with a HABTM relation. Albums cointains many Singles and Singles belongs to many Albums.
Model Album.php ( don't care about raisins: is my personal plugin for managing images )
App::uses('Raisin', 'Raisins.Model');
App::uses('PastryBehavior', 'Raisins.Model/Behavior');
class Album extends AppModel
{
public $name = 'Album';
public $actsAs = array(
'Tags.Taggable',
'Utils.Sluggable' => array(
'label' => 'title'
),
'Raisins.Pastry',
'SrEngine.Indexable',
'SrEngine.HitCounter'
);
public $hasOne = array(
'Image' => array('className' => 'Raisins.Raisin')
);
public $hasAndBelongsToMany = array(
'Single'
);
public $belongsTo = array(
'Users.User'
);
}
Controller AlbumsController.php ( edit part )
$this->request->data = $this->Album->read(null, $id);
$this->set('singles',$this->Album->Single->find('list'));
View
<?php echo $this->Form->input('Single',array('class' => 'span12','label' => 'Singoli' ) ); ?>
In the view I see the select populated from the values coming from the controller singles set, but the selected item, previously saved, is not highlighted.
Consider that in the database the habtm table contains the correct row populated with the data saved.
I read , somewehere in the middle, that some previous version of cake, prior to 2.3, used to have some problem related to habtm form select input.
I am very stuck at this because I have almost tried every known workaround without sorting any positive effect.
Any help, hint would be very appreciated.
Thanks in advance
Rudy

$_referenceMap Relationships with Zend_Db_Table_Abstract creates too many queries

This is my first time using Zend Framework for an application and i don't know if I completely have by head around Models.
I have four tables: shopping_cart, product, product_unit, distributor.
shopping cart has an cart_id, product_id, unit_id and dist_id (shopping cart joins on the other tables with their corresponding id).
Before Zend I would create a class like this:
class ShoppingCart
{
function getItems()
{
$sql ="select * from shopping_cart, product, product_unit, distributor
where
shopping_cart.product_id = product.id AND
shopping_cart.unit_id = product_unit.id AND
shopping_cart.dist_id = distributor.id AND
cart_id = xxx";
$items = $this->db->getAll($sql);
}
One query to get all the information from the joined tables.
When I set up the relationship mapping in Zend_Db_Table_Abstract:
My Shopping Cart Model:
class Application_Model_ShoppingCart
{
function __construct()
{
$this->ShoppingCartTable = new Application_Model_DbTable_ShoppingCart();
}
function getItems()
{
$cart_items = $this->ShoppingCartTable->getItems($this->GetCartId());
return $cart_items;
}
}
class Application_Model_DbTable_ShoppingCart extends Zend_Db_Table_Abstract
{
protected $_name = 'shopping_cart';
protected $_rowClass = 'Application_Model_DbTable_ShoppingCart_Item';
protected $_referenceMap = array(
'Product' => array(
'columns' => 'product_id',
'refTableClass' => 'Application_Model_DbTable_Product',
'refColumns' => 'id'
),
'Distributor' => array(
'columns' => 'dist_id',
'refTableClass' => 'Application_Model_DbTable_Distributor',
'refColumns' => 'id'
),
'Unit' => array(
'columns' => 'unit_id',
'refTableClass' => 'Application_Model_DbTable_ProductUnit',
'refColumns' => 'id'
)
);
public function getItems($cart_id)
{
$where = $this->getAdapter()->quoteInto('cart_id = ?', $cart_id);
return $this->fetchAll($where);
}
}
In my controller:
$this->_shoppingCartModel = new Application_Model_ShoppingCart();
$items = $this->_shoppingCartModel->getItems();
IN my view :
foreach($this->items AS $item)
{
$item_product = $item->findParentRow('Application_Model_DbTable_Product');
$item_dist = $item->findParentRow('Application_Model_DbTable_Distributor');
$item_unit = $item->findParentRow('Application_Model_DbTable_ProductUnit');
}
when I have ten items in my cart the db profiler shows over sixty queries (WHOA) to view the cart items ( information across all four tables are displayed - product name, unit description, distributor name).
For each item it queries the shopping_cart, then querys the product table, then the product unit, then the distributor_table.
Is there a way to have this run as one query joining all the tables via Zend_Db_Table_Abstract relationships?
Will I have to go back to using db adapter in the my Application_Model_ShoppingCart class?
I want to abstract all data access to the table models (Application_Model_DbTable_ShoppingCart) and not have the Application_Model_ShoppingCart tied to a db handler.
Thanks in advance for advice, I love the Zend Framework but models are still hard for me to understand given the different conflicting ways people talk about using them.
In short, no, unfortunately it's not possible to get a set of table rows together with their relationships in a single query.
It's just that all methods dealing with relationships are defined for a row, not for a table.
But at least, you can form your sql with Zend_Db_Table_Select instead of writing it all manually.
Upd: Your code for fetching ShoppingCarts, in my opinion, should belong to the table (DbTable_ShoppingCart). So, the code you provided in the beginning could be transformed to the following:
class Application_Model_DbTable_ShoppingCart extends Zend_Db_Table_Abstract {
public function getItem($cart_id) {
$select = $this->select()
->from( array('sc' => 'shopping_cart'), array(Zend_Db_Select::SQL_WILDCARD) )
->join( array('p' => 'product'), 'sp.product_id = p.id', array(Zend_Db_Select::SQL_WILDCARD) )
->join( array('pu' => 'product_unit'), 'sp.unit_id = pu.id', array(Zend_Db_Select::SQL_WILDCARD) )
->join( array('d' => 'distributor'), 'sp.dist_id = d.id', array(Zend_Db_Select::SQL_WILDCARD) )
->where('sp.cart_id = ?', $cart_id)
->setIntegrityCheck(false);
return $this->fetchAll($select);
}
}