I am having trouble understanding how the model class can create a table in the database.
If I wanted a table with fields users and password, wherein CakePHP do I define these fields? It seems like, in the examples I've seen, that these fields are validated in the model. But where are they defined or initialized?
Thanks.
CakePHP models don't create the database tables for you. They are a means of accessing existing tables that you have created. If you follow CakePHP's naming conventions the models just automatically know which tables they relate to; singular CamelCased model names relate to plural snake_cased tables.
To create your database tables you can either do this manually, or use CakePHP schemas to manage the database structure. There's also the excellent CakeDC migrations plugin for managing changes to the database structure.
CakePHP Schema management
CakeDC Migrations plugin
When a model is used in CakePHP it can determine the fields that belong to the model by running a SQL DESCRIBE query on the database table. This is done using the Model::schema() method. Cake caches the query results so that it doesn't need to keep querying the database for this.
Yes , sure , You can control database's table with its fields ,Model name should be the same of table name , in any controller you can load
$this->loadModel('SomeModel')
$mydata=$this->tableName->find('all');
$myuse =$this->set('myuse' , $mydata);
in your view , The name of view should be the same of your Controller's function name:
<h1> <?php echo $myuse['tableName']['fieldName]; ?></h1>
It not required to same name model as database table
like my table name is users and i create a model User
then you can define this table with User model in model > User.php
<?php
App::uses('AppModel', 'Model');
class User extends AppModel
{
public $name = 'User';
public $useTable = 'users';
public $primaryKey = 'id';
}
Related
I have spend hours trying to find where this issue is.
I have a database called tapplicant with a corresponding TapplicantController and Tapplicant model.
The View folder is called Tapplicant.
When ever i query e.g : $count = $this->Tapplicant->find()
It always looks for database table 'tapplicants' , it adds a 's'.
I assume you mean to say you have a table name tapplicants, not a database.
By default, Cakephp uses the plural form of the model name. If you need to change the table name, use useTable.
In your Tapplicant model:
public useTable = 'tapplicant';
Suppose a table name of mysql database is 'admin' and controller name is 'userscontroller' and model name is 'user'.
How can I access the data from the table 'admin' in cakephp?
Please explain with code.
This is probably not the best solution but What I would do is make a new Model called 'Admin' specifically for that table, then in the UserController Call it, example:
In the UserController
$this->loadModel('Admin');
$this->Admin->find('all');
In the Admin Model
$useTable = 'admin';
My question is in relation this this answer.
https://stackoverflow.com/a/8773953/1297775
I have read at many places, what #deceze put as:
"To be quite honest, for any halfway complex application, relying on Cake's automagic handling of HABTM relationships can get quite fragile and hard to debug, so I always manage HABTM records myself."
Even the CakePHP book hints at it http://book.cakephp.org/2.0/en/models/saving-your-data.html#what-to-do-when-habtm-becomes-complicated
I want to ask, that when he says he 'manages' this HABTM records himself, does he...
1) Not create the HABTM relations in the models at all and create a model for the join table
OR
2) Create the HABTM relations in the models, but does not use it in the controller code by just using $this->FirstModel->JoinModel->saveAll($someData);
Thanks
Basically you need use a hasManyThrough relationship, which is essentially a HABTM setup manually which allows you to add extra data to the relationship (such as created/expiry dates). It's quite simple, you just need to create a model to join them and use the normal belongsTo and hasMany properties in the model, here is a simple user/membership/course setup:
class User extends AppModel {
public $hasMany = array('Membership');
}
class Course extends AppModel {
public $hasMany = array('Membership');
}
class Membership extends AppModel {
public $belongsTo = array('User', 'Course');
}
The only fields (at minimum) the memberships table needs is course_id and user_id.
You can now operate on the Membership model as a normal model, without Cake treating it as HABTM and using its auto-magic whenever you save records. You can also use cool counterCaches on the membership model to count how many users a course has etc.
i think i have all 'baked' all my relationships correctly but my 'related' table shows id's instead of values.
my db has 3 tables. candidates, qualificationlookups, qualifications.
qualificationlookups, links candidates to qualifications using the id of the candidate and the id of the qualification.
in my view view, for 'candidates', i have a 'related qualificationlookups' table. (generated by baking a candidate hasmany qualificationlookups and a qualificationlookups belongsto candidate relationship)
in my edit view, for 'qualificationlookups', i can correctly set up the candidates and qualifications fields as dropdowns so i know 'qualificationlookups's relationships are fine.
So how do i ask cakephp to list the name of the qualification (from 'qualifications' table) in the 'related qualificationlookups' table on a candidate's page?
i must be missing something...
could someone please point me in the right direction?
Thanks, Vauneen
Whenever CakePHP automagically fetches lists from your tables, it uses the id key for the value and the $displayField for the text.
If your table has a name or title field, CakePHP automatically displays it as the display field. So, either rename the field that you want as your display field (say, candidate_name to just name) or set the $displayField variable in your model:
class Candidate extends AppModel {
var $displayField = 'candidate_name';
}
HTH.
If there is no other data being stored in the qualificationlookups table, change the relationship to candidates -> HABTM -> qualifications.
To do this, you first need to drop the qualificationlookups table. Create a new table called candidates_qualifications with two indexes, candidate_id and qualification_id.
In your Candidate and Qualification models, define a HABTM Relationship. You do not need to make a new CandidatesQualification Model, cake will do it on the fly.
I saw the question can i use a database view as a model in django and tried it in my app, but that didn't work.
I created a view named "vi\_topics" manually and it had "id" column but I kept getting an error, even if I added "id" field explicitly, saying
"no such column: vi_topics.id"
Here is the definition of my model named Vitopic:
from django.db import models
class Vitopic(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
author_name = models.CharField(max_length=200)
author_email = models.CharField(max_length=200)
view_count = models.IntegerField(default=0)
replay_count = models.IntegerField(default=0)
tags = models.CharField(max_length=255)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
db_table = 'vi_topics'
Note: I use sqlite3.
Try this:
http://docs.djangoproject.com/en/dev/ref/models/options/#managed
managed
Options.managed
New in Django 1.1: Please, see the release notes
Defaults to True, meaning Django will create the appropriate database tables in syncdb and remove them as part of a reset management command. That is, Django manages the database tables' lifecycles.
If False, no database table creation or deletion operations will be performed for this model. This is useful if the model represents an existing table or a database view that has been created by some other means. This is the only difference when managed is False. All other aspects of model handling are exactly the same as normal. This includes
Adding an automatic primary key field to the model if you don't declare it. To avoid confusion for later code readers, it's recommended to specify all the columns from the database table you are modeling when using unmanaged models.
If a model with managed=False contains a ManyToManyField that points to another unmanaged model, then the intermediate table for the many-to-many join will also not be created. However, a the intermediary table between one managed and one unmanaged model will be created.
If you need to change this default behavior, create the intermediary table as an explicit model (with managed set as needed) and use the ManyToManyField.through attribute to make the relation use your custom model.
For tests involving models with managed=False, it's up to you to ensure the correct tables are created as part of the test setup.
id = models.IntegerField(primary_key=True)