CakePHP specify table-name - cakephp

I have a table called entries with an associated model called Entry. I stepped through the Blog-tutorial on the CakePHP website. It states that the table-name does not need to be specified because it is computed from the models name. ( Post => posts).
I doubt CakePHP knows that Entry is a non-conform noun when it comes to its plural form. So how can I set the table my model uses? I think CakePHP would otherwise try to access the table entrys.

The answer to your question is to specify it inside your model.
$useTable = 'yourtablename'
Although, as Wooble noted, your doubts are wrong.

Cakephp 3, use this instead:
https://book.cakephp.org/3.0/en/orm/table-objects.html
class MyTable extends Table
{
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('my_table');
$this->setAlias('MyTable');
}
}

Related

retrieve name from the other table

Is there an efficient way of retrieving the name by using select and join clause? I have a Note, NoteType and NoteStatus model. There are type and status field which will be stored as integer (representing the id of its respective model) inside Note model. NoteType and NoteStatus models have id and name fields.
foreach($notes as $note)
{
$type=NoteType::where('id',$note->type)->first();
$note->type=$type->name;
$status=NoteStatus::where('id',$note->status)->first();
$note->status=$status->name;
}
Model Relations
Setting up relations between your models would be the best way as you then don't need to re-invent the wheel each time you need to call the join. It will save you code in the long run.
More info on this here:
Laravel Eloquent Relationships
Query Builder
If you want to do this manually then it would be the same as if you ran the query in raw SQL:
$note = Note::join('NoteType','Note.NoteType_id','NoteType.id')
->select('Note.*','NoteType.Name as NoteName')
->first();
Now you can get all the info from $note
Note id = $note->id
NoteType Name = $note->NoteName
Obviously adjust this to your code but this should help you build your knowledge enough to work it out.
More info can be found here:
Laravel Query Builder Joins
Assume that Your model name is Note.php
assume in your notes table has note_status_id and note_type_id foreign key
Add Relationship in your main model Note.php
public function status()
{
return $this->belongsTo(NoteStatus::class);
}
public function notes()
{
return $this->belongsTo(NoteType::class);
}
You can retrieve data with relationship something like that
Note::with('status','notes')
->get()
For more info regarding laravel relationship Laravel Eloquent: Relationships

Cakephp 3 - Name convention in plurals

Maybe this is a simple question but I didn't find it in cookbook.
I am confused when name for some plural words with 'ies"
Example:
City -> Cities
Controller: CitiesController
Table: CitiesTable
Model: City ???? or Citie ???
Now both made error when I run the app. Error did't find model name.
Thank you for any answers and sorry if my English is too bad.
UPDATE June 28th 2016
I found the solution. In fact, I just kept their name:
Controller: CitiesController in src/Controller/CitiesController.php
Table: CitiesTable in src/Model/Table/CitiesTable.php
Model or Entity: City in src/Model/Entity/City.php
It works for me.
Thank for all.
General use of the controller/entity/table name is the singular form of the noun(in your case CityController, CityTable and City). This removes the confusions between the singular and plural.
Another good practice is that your tables in the DB to be named with the singular of the entity they represent (for example your table is city , therefore your Table class should be called 'CityTable') . So you can always relate the information in the source with the tables in the DB.
Update Aug 11th 2016
Hi all,
Sorry for long time to answer this. I was quite busy. I found out my answer. I didn't use the "city" table only. I used "m_cities" instead. (M means Master). So all my files and classes name are:
Model > Entity:
MCity.php
Class:
class MCity extends Entity{
...
}
Model > Table
MCitiesTable.php
Class:
class MCitiesTable extends Table
{
...
}
In the class MCity in file MCity.php. I defined the initialize with following content for declare the table name for CakePHP understand my table name.
parent::initialize();
//Define used table name in database
$this->table('m_cities');
So final content of this class is:
class MCity extends Entity
{
public function initialize(array $config)
{
parent::initialize();
//Define used table name in database
$this->table('m_cities');
}//end initialize
}//end class
All are worked OK for my project until now. Because of somebody didn't see my update in post so I re-update as answer for searching people.
Thank you all.

CakePHP's naming conventions on hasMany through (The Join Model)

After struggling with this inconvenience for a couple of weeks now, I've decided to open en now topic here. A topic that can help me, but I'm sure it will help some others with this same problem too!
I wonder how I should name the tables, controllers and models of a hasMany through table (thus with additional fields) and it's coupled tables. I tried to stick on the CakePHP naming conventions as discribed in it's cookbook, but this constantly gave me some "Object not found" errors. For practical reason, I'll show you my problem with a multiple-words named table. Perhaps that could be the reason of the problem? ;)
Situation
I have a fansite of a themepark and as you now, a themepark has many attractions. To ride an attraction, you must have a minimal height. Sometimes, small people can only ride it with an adult companion. But most of the time: you are allowed to ride the attraction because you just are tall enough ;)
Now I want to show the information of a specific attraction on my website. Name, content, photos, and so on. In addition to that information, I want to display my guests if they (or their kids) are tall enough to ride that attraction. It should appear like this way:
0m00 -> 1m00: not allowed
1m00 -> 1m30: allowed with an adult companion
1m30 -> 1m90: allowed
Database
I have two tables that are representing two objects: "attractions" & "attraction_accessibilities". In this case, I'm 100% sure the database names are correct.
Table "attraction_accessibilities" (id - name):
1 - Not allowed
2 - Allowed with an adult companion
3 - Allowed
Table "attractions" (id - name):
1 - Boomerang
2 - Huracan
3 - Los Piratas
4 - El Volador
...
Secondly, I should have another table between "attractions" and "attraction_accessibilities". This table should contain:
an id specific for each record
a link to the id of the "attractions" table (attraction_id)
a link to the id of the "attraction_accessibilities" table
(attraction_accessibility_id)
the additional information like "min-height" and "max-height"
I think I should name that table "attraction_accessibilities_attractions". It's a constriction of the two other tables, and I did it that way because CakePHP proposed it when you're making a HABTM association (http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasandbelongstomany-habtm).
But unfortunately, when I do call it that way, I've never succeeded to link those models in my application together.
Question
Is there anybody who've had the same problem but found a solution for it? If "yes": how should I name my database tables then and also important: how should I name my controller and model .php files?
Many thanks for the one who could help me and some other hopeless programmers ;)
If you use the HABTM relationship with unique set to keepExisting then you can name the table as you like and set the joinTable parameter according to he name you choosed
i.e. in your Attraction model
public $hasAndBelongsToMany = array(
'AttractionAccessibility' =>
array(
'joinTable' => 'attraction_accessibilities_attractions',
)
);
Instead if you're going to use the hasMany through relation then you can name the table as you like. In fact the so called "hasMany through" is just the concatenation of two hasMany relationships
so. If you name your table restrictions then
class Attraction extends AppModel {
public $hasMany = array(
'Restriction'
);
}
class AttractionAccessibility extends AppModel {
public $hasMany = array(
'Restriction'
);
}
class Restrictionextends AppModel {
public $belongsTo = array(
'Attraction ', 'AttractionAccessibility '
);
}

Retrieving data from referenced key table - Laravel-4

The structure of concerning tables is as follows (MySQL):
//Table Name : team
tid PK
team_name (varchar)
//Table Name : fixture
fid PK
home_team_id FK |_ both referenced to 'tid' from 'team' table
away_team_id FK |
My aim is to retrieve the team names. Considering this structure, I think I'll have to retrieve home_team_id and away_team_id and then do something like
Fixture::where('tid','=',$home_team_id)->get();
My question is, is this the correct way to accomplish what I aim to do?
and
should this be done from the controller? (if so, then I'll have to do two queries from same function)
First, rather than having your primary keys be tid and fid, just keep them both as id. This is not only best practice, but will allow you to more easily use Laravel's Eloquent ORM as it by default assumes your primary key column is named id.
Second thing, make sure your table names are in plural form. Although this is not necessary, the example I'm about to give is using Laravel defaults, and Laravel assumes they are in plural form.
Anyway, once you've 'Laravelized' your database, you can use an Eloquent model to setup awesome relationships with very minimal work. Here's what I think you'd want to do.
app/models/Team.php
class Team extends Eloquent {
// Yes, this can be empty. It just needs to be declared.
}
app/models/Fixture.php
class Fixture extends Eloquent {
public function homeTeam()
{
return $this->belongsTo('Team', 'home_team_id');
}
public function awayTeam()
{
return $this->belongsTo('Team', 'away_team_id');
}
}
Above, we created a simple model Team which Laravel will automatically look for in the teams database table.
Second, we created model Fixture which again, Laravel will use the fixtures table for. In this model, we specified two relationships. The belongsTo relationship takes two parameters, what model it is related to, in both cases here they are teams, and what the column name is.
Laravel will automatically take the value in away_team_id and search it against the id column in your teams table.
With just this minimal amount of code, you can then do things like this.
$fixture = Fixture::find(1); // Retrieves the fixture with and id of 1.
$awayTeam = $fixture->awayTeam()->first(); // var_dump this to see what you get.
$homeTeam = $fixutre->homeTeam()->first();
Then you can proceed as normal and access the column names for the tables. So say you have a 'name' column in the teams table. You can echo out the the home team name from the fixture like so.
$fixture = Fixture::find(1); // Get the fixture.
echo $fixture->homeTeam->name;
It's nearly 2AM, so there might be an error or two above, but it should work.
Make sure you check the docs for Eloquent, especially the bits relating to relationships. Remember to name your columns and tables in the way Laravel wants you to. If you don't, there are ways to specify your custom names.
If you want to get even more fancy, you can define the inverse relationship like this on your Team model.
app/models/Team.php
class Team extends Eloquent {
public function fixturesAtHome()
{
return $this->hasMany('Fixture', 'home_team_id');
}
public function fixturesAway()
{
return $this->hasMany('Fixture', 'away_team_id');
}
}
Then to get all of a particular team's home fixtures...
$team = Team::find(1); // Retreive team with id of 1;
$homeFixtures = $team->fixturesAtHome();

How to use function find('list') get data from other table beside its model?

I am new of Cakephp I stuck by using function find('list') to get data from other table inside User model. Example i have User Model want get List of group andgroup has table "groups" but i don't want create php files for group controller and group model.
<?php
//User model
class User extends AppModel {
public function getGroupList() {
/* Here i want return list all group by use function find('list')
* but group has table name "groups" and i don't want use sql query string.
* Note that group i don't create files php in controller and model.
*/
}
}
?>
Why don't you want to create a groups controller? Doing things cakes way will often make your life easier.
There will be an extra sql query (or join at least) involved in either case.

Resources