cakePHP hasOne relationship not auto completing dropdown field - cakephp

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. :)

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 relations in Database

I baked two controllers users and moves. Now I want to link the moves the users is linked to (only one). The bake did most of the work for me (thank god).
<td><?= $user->has('move') ? $this->Html->link($user->move->name, ['controller' => 'Moves', 'action' => 'view', $user->move->id]) : '' ?></td>
It actually show nothing. I have a foreign key in my database and the move 1 is correctly linked to the user.
foreign key in users "move_id" - primary key in the moves is "id"
I get no error and also no debug call. Any ideas?
It's showing nothing because in your ternary operator, it's executing this next portion >>>>>>>
: ''
and therefore prints a blank. Your $user object probably doesn't have a field called "move".
You need to check the following:
Are associations defined for these two models?
In your controller, where you're fetching this $user before doing $this->set(.....), did you mention "contain"? Since you need to access the Users as well as the Moves models?
For example:
// If you're trying to find all users records
$users = $this->Users->find('all')
->contain(['Moves']);
// For a single user record
$users = $this->Users->get($this->Auth->user("id"))
->contain(['Moves']);
Hope this helps.
Peace! xD

Cakephp Multiple Relations to the Same Model

I'm working on a real estate application, where one Home can be listed by either one or two Realtors. As such, I'm trying to follow the example given in section 3.7.6.7 of the Cake Cookbook. Here's what's going on in my Realtors model:
class Realtor extends AppModel {
var $primaryKey = $num;
var $name = 'Realtor';
var $hasMany = array('Homes' =>
array('className' => 'Home',
'foreignKey' => 'realtor_num',
...),
'CoListedHomes' =>
array('className' => 'Home',
'foreignKey' => 'realtor2_num',
...)
);
This completely breaks the application, with the error message "Database table co_listed_homes for model CoListedHomes was not found."
Referring back to the Cookbook 3.7.6.7, second example, it seems clear that they shouldn't have or need separate "messages_received" and "messages_sent" tables in their database, so what is it that I'm doing wrong?
ETA: Weirdly (to me) I think I got the relationships to work by swapping around the order of the arrays: the first array, on foreignKey = realtor2_num, is called 'ListedHomes', the second array, on foreignKey = realtor_num is called 'Home'. I suppose my question then is, why should the order matter, and why should Cake started talking about unfound database tables in some circumstances?
Just in case, I'd have built this as a categories setup. Then created a join table between Category and Home.
Then you can have a hasAndBelongsToMany beween both categories and homes
"Database table co_listed_homes for model CoListedHomes was not found" probably refers to you not having a model defined for CoListedHomes that points to the homes table.
I would, however, code this as a n:m otherwise known as hasAndBelongsToMany, as stated by DavidYell.

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