Does anyone knows how to get table name from model name? Also I want to get all column names and its types of that model/table name. Is it possible to get such details of given model name?
Thanks.
Table Name
To get the table, see
$this->Model->table
Or check the model for the class variable $useTable. If that's undefined, then you can infer it from the name of the model:
$tableName = Inflector::tableize($this->Model->alias);
See the Inflector documentation for similarly useful methods.
Columns
Take a look at the schema method of the Model class. For example:
var_dump($this->Model->schema());
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';
I use the Zend 2 Framework to build my web application. I implemented my database table models by this tutorial: http://framework.zend.com/manual/2.1/en/user-guide/database-and-models.html
I have a many-to-many relationship between two models in my database. To get data from them I googled and found this link: http://mattmccormick.ca/2010/04/24/how-to-easily-create-models-and-table-relationships-in-zend-framework/
The problem is that all the table models extends from Zend_Db_Table_Abstract in the example. I don't know how to get data from the models.
I have a table containing votings, every voting has a unique hash id. Every voting also has tags. Therefore I defined a table tags with all the tags available and a voting_tag_map where all many-to-many relationships are mapped.
What I have tried so far is the following, that's code from my VotingTable class:
public function getTagsByVoting($votingHash){
$select = $this->tableGateway->getSql()->select();
$select->from(array('v' => 'voting'))
->join('voting_tag_map', 'v.voting_id=voting_tag_map.voting_id')
->join('tags', 'voting_tag_map.tag_id=tags.tag_id');
$resultSet = $this->tableGateway->selectWith($select);
return $resultSet;
}
It says then:
Since this object was created with a table and/or schema in the constructor, it is read only.
Thats because of the from() method. If I delete the from() method, it says:
Statement could not be executed
Can anyone help me please?
Since this object was created with a table and/or schema in the constructor, it is read only.
This error is because you are trying to set the table name in the from clause, but it's already been set in the contructor of the TableGateway, and you can't change it once set.
If you really need to do this then you can extens AbstractTableGateway yourself then you won't have to add a string tablename to the contructor, but you don't really need to use an alias on your main table...
The SQL error you get when you comment out the from() method will be due to your referencing the votes table as it's alias 'v' in your join, when you are not using the alias v, try changing it to 'voting.XXX' from 'v.XXX'
I made a model for my pre-set database table.
Now in my controller's action, I have:
$this->loadModel('Preset');
$preset = $this->Preset->find();
Each pre-set row in the table has multiple fields, I want the $preset variable to have all the information from one row. How would I search for the row by name and then grab all other fields from it?
Assuming your model is valid:
$preset = $this->Preset->findAllByName('foo');
What you're trying to accomplish is really quite simple and well-covered in CakePHP's documentation.
The CakePHP cookbook presents the following (http://book.cakephp.org/2.0/en/models/model-attributes.html#schema):
Contains metadata describing the model’s database table fields. Each field is described by:
name
type (integer, string, datetime, etc.)
null
default value
length
Some of these are self-explanatory, but here are my questions:
Name - what's the purpose of this? Is the cakebook just unclearly saying that the other values will be in an array stored under the name of the field, or is this a key that can give a different name, and what would it be for?
Type - I understand what type is, but could someone give me a full list of the options? Hard to say what it is if I don't know what my options are. Are they based on typical database types, or form types, or what?
Null - Is this the same as the not null option of a database? Basically just requiring a value or throwing an error?
I would like to write out each of my table's schemas for various reasons, but I'm a little stuck because of these questions.
Thank you for your help!
you can find most of the answers to your questions yourself.
either you debug the returned data from
$this->Model->schema();
which contains all the fields above.
Or you use the cake shell to create/dump a schema file in /Config/Schema:
cake schema generate
this way you can look at what cake creates.
So if you create a dummy table "foobars" with all kind of field types you will have a full schema reference out of the box for the current cake version you are using. Also you will see your database reflected as cake sees it. meaning: if you set one field to "default not null", the other as "default null" you will see what "null" means. And what type of the database matches what type in cake.
And yes, name is the fieldname. it should be the array key itself, though.
I'm trying to follow table and class name conventions, but I am not sure if CakePHP is linked with the correct table. For example, posts_tags table should be with the class name PostTag. Is there a function I can use to check this?
First, posts_tags would be associated with a model PostsTag (notice the pluralized Posts).
Second, the table it's using is available in Model->table, which is set in __construct().