How can I save multiple images in the secondary table?
My first table is Car, which has the fields:
id
title
featured_image and
My secondary table is Gallery, which has the fields:
id
car_id
gallery_images
Model Car.php
class Car extends AppModel
{
var $name='Car';
var $hasOne = array(
'Gallery' => array('className' => 'Gallery',
'foreignKey' => 'car_id'
));
}
Model Gallery.php
class Gallery extends AppModel
{
var $name='Gallery';
var $belongsTo = array(
'Car' => array('className' => 'Car',
'foreignKey' => 'car_id',
)
);
}
CarController.php
$this->Car->saveAll($this->data)
To save multiple images associated to a car, first you have to modify your Car model to:
class Car extends AppModel
{
var $hasMany = array(
'Gallery' => array(
'className' => 'Gallery',
'foreignKey' => 'car_id'
));
}
or just
class Car extends AppModel
{
var $hasMany = array('Gallery'); //simplified version, as fields follow convention
}
Then you have to structure you data as follows:
$data = array(
'Car' => array('title' => 'Volvo'),
'Gallery' => array(
array('gallery_images' => '/path/image1'),
array('gallery_images' => '/path/image1'),
array('gallery_images' => '/path/image3'),
),
);
In your view, the form should have the following structure:
echo $this->Form->create('Car', array('action' => 'add'));
echo $this->Form->input('Car.title');
echo $this->Form->input('Gallery.0.gallery_images');
echo $this->Form->input('Gallery.1.gallery_images');
echo $this->Form->input('Gallery.2.gallery_images');
echo $this->Form->end();
In your CarControllers::add() you can save all four records (one car and three images) with:
$this->Car->saveAll($this->request->data);
Please consider renaming your fields to represent better your data. For example, gallery_images should be renamed to something like image_filename, and model Gallery to Image of Photo.
As per Car.featured_image, it can hold a foreign key for galleries, or you can move this field on to the galleries/images table and make it a field named type (holding 'featured, normal, etc'), or perhaps a boolean named is_featured.
Related
Will the given below code can work where Mark and Subject are two model with which Student model associate through hasMany association
class Student extends AppModel
{
public $name = 'Student';
var $hasMany = array(
'Mark' => array(
'className' => 'Mark',
'foreignKey' => 'student_id'
),
'Subject' => array(
'className' => 'Subject',
'foreignKey' => 'student_id'
)
);
}
yes you can relationship with many tables
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html
Find conditions with hasMany model
I have a few Tables/Models and I want to show the content of the models in one view.
My Schema:
My Models:
Adress:
var $name = "Adress";
public $belongsTo = array("Customer", "Country");
public $hasAndBelongsToMany = array(
'Contactperson' => array(
'className' => 'Contactperson'
)
);
ContactPerson:
var $name = "Contactperson";
public $hasAndBelongsToMany = array(
'Adress' => array(
'className' => 'Adress'
)
);
Country:
var $name = "Country";
public $hasMany = "Adress";
Customer:
var $name = "Customer";
public $hasMany = array(
'Adress' => array(
'className' => 'Adress',
'order' => array('Adress.mainadress DESC', 'Adress.created DESC')
)
);
My CustomerController:
$customer = $this->Customer->findByid($customerId);
$this->set('customer', $customer);
The return value is the content of the customer and the adress table but I want to get the content of every table.
I want to get a array with the content from customers, addresses, contactpeople and countries.
Thanks for helping.
Once you setup and linking each table correctly (with foreign key and db design), then you can retrieve all the related field easily with CakePHP.
Read up on CakePHP containable.
http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html
Recursive will also works, but higher recursive value can hurt your system if its getting too big.
I'm building an MMA (mixed martial arts) website with CakePHP. I've got a fights table in my database that has three columns at its simplest: id, fighter_a, and fighter_b.
I'm having trouble getting my head around what type of relation my Fight model would have with my Fighter module. Am I right in thinking fighter_a and fighter_b would be two hasOne relations?
I tried this with the following in my Fight model:
<?php
class Fight extends AppModel {
public $name = 'Fight';
public $hasOne = array(
'FighterA' => array(
'className' => 'Fighter',
'foreignKey' => 'fighter_a'
),
'FighterB' => array(
'className' => 'Fighter',
'foreignKey' => 'fighter_b'
)
);
}
And then this in my Fighter model:
<?php
class Fighter extends AppModel {
public $name = 'Fighter';
public $hasMany = array(
'Fight'
);
}
But this threw an error in my CakePHP application when calling $this->Fight->findById($id) (where $id was the ID of a fighter):
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Fight.fighter_id' in 'field list'
How can I link my models so that I can call all fights a fighter has been in?
Distilled from conversation under the question, the solution would be this:
Rewrite the $hasMany in your FighterModel to look like:
public $hasMany = array(
'Fight' => array(
'className' => 'Fight',
'finderQuery' => 'SELECT * FROM fights AS Fight WHERE Fight.fighter_a_id = {$__cakeID__$} OR Fight.fighter_b_id = {$__cakeID__$};'
)
);
Hi
I'm trying to to do pagination from the KindsController but with data from the LinkModel
Tables :
Kinds links
id id
name title
order url
kind_id
order
model:
class Kind extends AppModel{
var $name = 'Kind';
var $hasMany = 'Link';
}
controller:
class KindsController extends AppController{
var $name = 'Kinds';
var $paginate = array(
'Kind'=> array(
'limit' => 12,
'order'=> array('Kind.order'=>'ASC')
)
);
}
desired result (view) ;
Kind.name ( numbers of links )
links.name
........
..........
Kind.name ( numbers of links )
links.name
........
..........
i'm using the kinds.order and links.order to control how the data is displayed in the view
from an admin area.
i need to retrive data from the kinds table ordered by kinds.order ASC and the number of links, and the links data ordered by links.order
thanks
You can define order in the relation like this:
class Kind extends AppModel{
var $hasMany = array(
'Link' => array(
'className' => 'Link',
'order' => 'Link.name'
),
);
}
I want to model the following simple relationship:
One Passenger belongs to a Car; One Car has many Passengers.
The passenger table has an id and Car_id column, the Car table has one id column.
My models look like this:
<?php
class Passenger extends AppModel {
var $name = 'Passenger';
var $belongsTo = 'Car';
} ?>
and
<?php
class Car extends AppModel {
var $name = 'Car';
var $hasMany = array (
'Passenger' => array (
'className' => 'Passenger',
'foreignKey' => 'car_id'
)
);
}
?>
and my add Passenger .ctp looks like this:
<?php
echo $this->Form->create('Passenger');
echo $this->Form->input('car_id');
echo $this->Form->end('Save');
?>
BUt when I access the page to add a passenger, all I see is an empty drop down box. Is there an additional step I must take in order to populate the dropbox with all cars?
First off, you have forgotten to mention the belongsTo relation in your Passenger model:
<?php
class Passenger extends AppModel {
var $name = 'Passenger';
var $belongsTo = array('Car');
}
?>
Next, in the corresponding action of your controller, you will need to obtain a list of all the cars from the database, and set it to the plural form of the model's variable ($cars). You would do that like so:
$cars = $this->Passenger->Car->find('list');
$this->set(compact('cars'));
This will convert the car_id input field into a drop down list with the populated information.
HTH.
The Passenger will only know about the car with which it is associated - at this point, none.
In the add method in the passenger controller, do
$this->Car->find('list');
and pass the result into your view:
$this->set('cars',$cars);
In the view, give the $cars variable as the value for $options in the field declaration:
echo $this->Form->input('car_id', array('options' => $cars));
Alternatively, you can do something like:
echo $this->Form->input('Car.id', array('options' => $cars));
$this->CompanyCashback->bindModel(array('belongsTo' => array(
'CompanyBranch' => array('className' => 'CompanyBranch', 'foreignKey' => false, 'conditions' => array('CompanyCashback.publisher_id = CompanyBranch.publisher_id && CompanyBranch.branch_type = "online" ')),
'PersonalInformation' => array('className' => 'PersonalInformation', 'foreignKey' => false, 'conditions' => array('CompanyCashback.publisher_id = PersonalInformation.user_id')),
'Country' => array('className' => 'Country', 'foreignKey' => false, 'conditions' => array('PersonalInformation.country_id = Country.id')),
'User' => array('className' => 'User', 'foreignKey' => false, 'conditions' => array('PersonalInformation.user_id = User.id')))
));