CakePHP 2.x Accessing Another Model - cakephp

Noob to cakePHP here.
Bicycle belongsTo Stand
Stand hasMany Bicycle
Stand belongsTo Station
Station hasMany Stand
I've baked the CRUDs and now I want to display the station name when I view a bicycle. How should I define the bicycle - station relationship? Or what do I need to do to include the station model in the bicycles controller?
What I've tried so far. Which throws a "Notice (8): Undefined index: Station":
BicyclesController.php:
public function index() {
$this->Bicycle->recursive = 2;
$this->set('bicycles', $this->Paginator->paginate());
}
index.ctp:
<?php echo $this->Html->link($bicycle['Station']['name'].$bicycle['Stand']['stand_no'], array('controller' => 'stands', 'action' => 'view', $bicycle['Stand']['id'])); ?>
Help!

Since you have no direct relationship from Bicycle to Station, its not going to be available within $bicycle['Station'] Its more likely to be available in $bicycle['Stand']['Station]['name'].
In order to see if its actually being returned with recursive set as it is, I would run the query again and call $debug($bicycle);die; in the following order.
public function index() {
$this->Bicycle->recursive = 2;
$bicycles = $this->Paginator->paginate();
$debug($bicycle);die;
$this->set(compact('bicycles'));
}
This will kill the model at this point and display an array of results returned. You will then see where the information you want is, and how to access it. Just remove the $debug line when finished.

Related

CakePHP 4.x hasMany foreach

I'm having some difficulty getting results from a hasMany relationship in my view file. I'm not getting any errors from the debug console so I'm not sure where I'm tripping up.
I think that maybe I'm not referencing correctly in the view file (I'm not really sure how to write a foreach function for this), if I'm not setting it up correctly in the table file, etc.
2 tables: "Clinicas", "Tratamientos" (services)
Goal: On a Clinic's profile page, I need to show the information for that Clinic (location, phone number, etc.) & list all of the services associated with that clinic.
ClinicasTable.php
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('clinicas');
$this->setDisplayField('name');
$this->setPrimaryKey('clinic_id');
$this->addBehavior('Timestamp');
$this->hasMany('Tratamientos', [
'foreignKey' => 'clinic_id',
'joinType' => 'INNER'
]);
}
ClinicasController.php
public function view($id = null)
{
$clinica = $this->Clinicas->get($id, [
'contain' => ['Tratamientos']
]);
$this->set(compact('clinica'));
}
view.php
<?php foreach($clinica as $tratamiento): ?>
<?= h($clinica->tratamiento->name)?>
<?php endforeach;?>
I've had a look at the documentation for associations but can't figure out how to get to data from my tables. I could always just do an ajax query through php functions, but I'd really like to do it right using CakePHP. Any help would be much appreciated!!!
Even though iterating is pretty much just basic PHP, the example in the book could be a little more helpful and show iterating over and using the associated data, even though it should already give you a good idea where you data lives.
You are obtaining a single entity (an instance of Clinica), so you cannot and should not iterate over it. What your code will do is iterate over all public properties of the entity object, which is not what you want, and won't do anything in your case, as by default entities do not have any concrete public properties.
The data of the association will be found on the association property of the Clinica entity, which, unless specifically configured otherwise via the association's property option, is the plural, underscored, lowercased variant of the association name, so for Tratamientos that would be tratamientos.
It should be noted that you really should stick to US english here for namings, as all the inflector magic around naming conventions is designed to work with that, names in other languages can easily cause mismatches/problems when they cannot be inflected properly!
Long story short, you iterate the $clinica object's tratamientos property, that's where the hasMany associated data lives:
<?php foreach($clinica->tratamientos as $tratamiento): ?>
<?= h($tratamiento->name)?>
<?php endforeach;?>
If you are unsure about a data structure, debug it: debug($clinica)

cakePHP hasOne relationship not auto completing dropdown field

I'm trying to implement a hasone relationship between 2 models, but I can't have the 'add' form autocomplete with the possible options in the second model (the one that belongsTo the first one). This is my code:
- model 1: item.php
<?php
class Item extends AppModel{
var $name = 'Item';
var $primaryKey = 'id';
var $hasOne = 'CoverImage';
}
?>
- model 2: cover_image.php
<?php
class CoverImage extends AppModel{
var $name = 'CoverImage';
var $primaryKey = 'id';
var $belongsTo = array(
'Item' => array(
'className' => 'Item',
'foreignKey' => 'item_id'
));
}
?>
- add view of model 2: add.ctp
<?php echo $this->Form->create('CoverImage',array('url' => array('controller' => 'admins', 'action' => 'add')));?>
<fieldset>
<legend><?php __('Info'); ?></legend>
<?php
echo $this->Form->input('item_id');
echo $this->Form->input('description');
?>
</fieldset>
<?php echo $this->Form->end(__('Create', true));?>
For what I see in Cake's documentation, with this relationship, in the add view I should see a dropdown list in the item_id field to be able to select to which item does this CoverImage belongs to, but the dropdown is empty (and yes, I have some items in the items table already).
Maybe I'm missing something or I've done something wrong, but I can't figure it out. Thanks so much in advance for any clues!
EDIT
One think I've just realized is that if I do this:
echo $this->Form->input('item_id', array('type'=>'text'));
instead of this:
echo $this->Form->input('item_id');
I can add/edit the *item_id* field, I can see its value in the text box. However, if I leave the other one, I just see an empty dropbox and when I try to add/edit a CoverImage, it doesn't work, it just shows an empty white page, not even with errors...
Maybe this is a lead to something...
In order for that to work you have to create a list of possible options in the controller. That does not happen automatically.
public function add() {
$items = $this->CoverImage->Item->find('list');
$this->set(compact('items'));
}
The FormHelper only automatically infers that the field item_id should be populated by the options in the variable $items (plural, no _id).
Do be careful that Items that already haveOne CoverImage should not be part of that list. find('list', array('conditions' => array('CoverItem.id' => null))) will probably* take care of that, but you'll need to recheck just before saving as well, or you need to rethink your associations.
* Not sure off the top of my head whether that'll work for 'list' searches.
EXCELLENT QUESTION. You've run afoul of a disingenuous feature of Cake's associations:
Considering you defined the relationship as hasOne? Guessing at the trace but Cake probably even correctly inferred your preference for list functionality. You got your automagic list...
...of One.
$hasOne is pretty exclusive like that. It "uses up" those "has" relationships (it's makes the relationship a de facto Singleton - so Users only have 1 Profile <-> Profile only has 1 User). Consider - Database can have many configurations, but Dbo will only ever have one Connection at a time and Connection will only have one Dbo. Thus -> hasOne is for marrying two Models til die() do they part.
-- So it doesn't get used nearly as much as hasMany and belongsTo.
For your purpose, you probably want to change to a different association.
Adding an additional $this->Item->find doesn't really fix what's wrong (and I wouldn't recommend it, unless you're mostly done with both models/controllers, or you actively want things to start getting weird fast.)
Also, changing how you call the Form Helper methods - if you return a 'list' type fetch from a find, Cake will automatically produce an option list out of it. What's actually happening is, you're sneaking around your Model on a very thin margin of View functionality. That's why specifying the input type to "break the magic" tends to be discouraged (which, you totally can if you want. Just understand what's actually happening, or: see, weird, fast.)
But you might want to rethink how you've associated your models - wouldn't it also be correct to say, each Item belongsTo a CoverImage (same as each CoverImage belongs to an Item) -- because you have a form expressly permitting a CoverImage to select an Item, any Item, to be displayed with? You'll probably get better results.
HTH. :)

cakephp: how to access another model, which has no association

I've 3 tables:
project, amenities and project_amenities.
I add project form only, i want to add new amenities, which first added to amenities table and then project_amenities table.
So I defined these relations:
Project hasMany Amenities
Amenity belongsTo Project
I declared in projects_controller:
var $uses = array('Amenity');
This gives error for my add action here
if (!empty($this->data)){
$this->Project->create();
as
Notice (8): Undefined property: ProjectsController::$Project
[APP\controllers\projects_controller.php, line 60]
edit
Also, I cannot do:
$this->Amenity->create();
$insertData = array('name' => $this->data['Project']['Amenity'][$i]['name']);
$this->Amenity->save($insertData);
I wouldn't recommend using $uses = array() as it defies the purpose of having model relationships. Plus it adds a fair bit of overhead too.
You should use your model relationships where ever possible:
$this->Project->create();
$this->Project->Amenity->create();
$this->Model1->Model2->Model3->Model4->create();
If you want to use an unrelated model, you can do:
$this->loadModel('NewModelName');
$this->NewModelName->action();
Check out http://book.cakephp.org/view/1031/Saving-Your-Data and http://book.cakephp.org/view/992/loadModel
I would like to suggest you to use $this->loadModel('ModelName'); in the function where you needed. Then you can use the model as below
$this->ModelName->function();
By using $uses array it make available to entire controller. Which will affect your performance.
you must specify the project model in your $uses variable:
$uses = array('Project','Amenity');
but I'm curious why do you have three tables when you need one to many relationship?
Solution for :save for hasAndBelongsToMany
I created
model: projects_amenity.php which has relation belongsTo project and amenity.
model: projects has relation hasAndBelongsToMany with amenity
controller: projects_amenities_controller
and in code:
$this->loadModel('ProjectsAmenity');
for($i = 0; $i< count($this->data['Project']['Amenity']);$i++)
{
$this->Amenity->create();
$insertData = array('name' => $this->data['Project']['Amenity'][$i]['name']);
$amenity = $this->Amenity->save($insertData);
$amenityid = $this->Amenity->getLastInsertID();
$projectid = $this->Project->getLastInsertID();
$this->ProjectsAmenity->create();
$data['AmenityArr'] = array('project_id' => $projectid,'amenity_id' => $amenityid );
$this->ProjectsAmenity->save($data['AmenityArr']);
}

CakePHP HABTM: Editing one item casuses HABTM row to get recreated, destroys extra data

I'm having trouble with my HABTM relationship in CakePHP.
I have two models like so: Department HABTM Location. One large company has many buildings, and each building provides a limited number of services. Each building also has its own webpage, so in addition to the HABTM relationship itself, each HABTM row also has a url field where the user can visit to find additional information about the service they're interested and how it operates at the building they're interested in.
I've set up the models like so:
<?php
class Location extends AppModel {
var $name = 'Location';
var $hasAndBelongsToMany = array(
'Department' => array(
'with' => 'DepartmentsLocation',
'unique' => true
)
);
}
?>
<?php
class Department extends AppModel {
var $name = 'Department';
var $hasAndBelongsToMany = array(
'Location' => array(
'with' => 'DepartmentsLocation',
'unique' => true
)
);
}
?>
<?php
class DepartmentsLocation extends AppModel {
var $name = 'DepartmentsLocation';
var $belongsTo = array(
'Department',
'Location'
);
// I'm pretty sure this method is unrelated. It's not being called when this error
// occurs. Its purpose is to prevent having two HABTM rows with the same location
// and department.
function beforeSave() {
// kill any existing rows with same associations
$this->log(__FILE__ . ": killing existing HABTM rows", LOG_DEBUG);
$result = $this->find('all', array("conditions" =>
array("location_id" => $this->data['DepartmentsLocation']['location_id'],
"department_id" => $this->data['DepartmentsLocation']['department_id'])));
foreach($result as $row) {
$this->delete($row['DepartmentsLocation']['id']);
}
return true;
}
}
?>
The controllers are completely uninteresting.
The problem:
If I edit the name of a Location, all of the DepartmentsLocations that were linked to that Location are re-created with empty URLs. Since the models specify that unique is true, this also causes all of the newer rows to overwrite the older rows, which essentially destroys all of the URLs.
I would like to know two things:
Can I stop this? If so, how?
And, on a less technical and more whiney note: Why does this even happen? It seems bizarre to me that editing a field through Cake should cause so much trouble, when I can easily go through phpMyAdmin, edit the Location name there, and get exactly the result I would expect. Why does CakePHP touch the HABTM data when I'm just editing a field on a row? It's not even a foreign key!
From the CookBook the 1st problem is:
By default when saving a
HasAndBelongsToMany relationship, Cake
will delete all rows on the join table
before saving new ones.
I am not quite sure why Cake is trying to save the HABTM data even though you don't have a foreign key in your data, but there is an easy solution for that. Simply destroy the association for the save call:
$this->Location->unbindModel(
array('hasAndBelongsToMany' => array('Department'))
);
I'm thinking of one reason why this might be happening. When you retrieve Location, you also retrieve locations_departments data. And when you do a save($this->data) it looks for models in the array and saves them.
A way to solve this is setting the recursive attribute (of a model) to -1 or 0 (try, I'm not sure, just print out the data to see what comes out). You can set it in the model: var $recursive = -1; or in the controller method (action): $this->ModelName->recursive = -1;
More about recursive: http://book.cakephp.org/view/439/recursive
It's really similar to what harpax suggested, just if you don't need that data, tell it to Cake, so that it won't fetch it.
Trouble is that when saving your Location, you gave the save method an array containing all the DepartmentsLocations too. Thus CakePHP destroys everything and try to recreate it.
This is a common mistake with cake since it will often pull far too many results for you.
Be sure to pass only the data that needs to be saved, or better to fetch only the datas you need.

CakePHP assocation question

I'm creating a small timesheet application. Timesheets have athletes, and each athlete has personal split times (like in running, or race car driving)
An Athlete hasMany Run, a Run belongsTo Athlete, An Athlete hasAndBelongsToMany Timesheets (and vice versa). A Timesheet hasMany Run, and finally a Run belongsTo Timesheet.
When I'm adding new runs in my view, I'm unable to get anything but the athlete_id in the select box. I'd really like to have their names instead. Instead of
<?php echo $run['athlete_id'];?>, I've tried <?php echo $athlete['Athlete']['last_name'] ?> but I can't seem to get it to work. Any help would be greatly appreciated. Thanks!
Without knowing exactly how you are building your forms/data it is hard to tell, but how I would do it is.
In the RunController add
$athletes = $this->Run->Athlete->find('list');
$this->set('athletes', $athletes);
and then in the View use this form helper line.
<?php echo $form->input('Run.athlete_id', array('type' => 'select', 'options' => $athletes)); ?>
This should work, there is also a way to use 'compact' to make it a little easier but the above should work fine.
---- BEGIN EDIT ----
I did a little research and found the compact method.
In your RunController use
$athletes = $this->Run->Athlete->find('list');
$this->set(compact('athletes'));
and then in your View use
<?php echo $form->input('Run.athlete_id'); ?>
and the form helper will automatically find the compacted Athlete array and build the select.
---- END EDIT ----
Hope this helps.
Cheers,
Dean
Try printing out the content of the $run: print_r($run) and see if the ['Athlete'] is there.
If not, you might have to manually contain the Athlete model when you do your run query:
$this->Run->contain('Athlete');
Don't forget to use the displayField property of the Model class i.e.
<?php
class Athlete extends AppModel {
public $name = "Athlete";
public $displayField = "name"; // the field name that has the athletes name in it
}
?>
http://book.cakephp.org/view/438/displayField

Resources