cakePHP renaming Database names - cakephp

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';

Related

Can we retrieve the field names from the models.py based on the column names in table from the database in a django project?

models.py: class Name(models.Model): name=models.TextField(db_column="Name_of_the_persion")
like this i defined the model, so that table 'Name' will be created on the database side, with column name as "Name_of_the_persion".
My requirement is like,I need to insert the new row in the table based on the django model field names instead of columns of tabel.
If any one knows, pls help me.
I tried inserting the data into table, by using psycopg2. But this was work in the case of pgadmin point of view only, that means its takes column names of database, instead of model fileds .

Many-to-Many relationship in Zend 2 Framework

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'

Load database table in controller cakephp

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.

Adding antoher fields in TranslateBehavior in CakePHP

I use TranslateBehavior in my app.
Model translate fields like name, content and slug. The table has many records.
...and now I must add antoher field to this table and I have problem. When I added field name to actsAs in model, my records return empty results. Why?
How add another field to Translated model after fact?
At first, I suggest you try to clean the model cache (delete files from path/to/project/app/tmp/cache/models.
I ran into the same problem.
The solution was to execute a SQL query to create the new translated fields that should have been created before.
I had a title tarnslator field before. Now I want to add a company translated field.
My i18n table is translation.
My model is Speaker.
Here is the SQL query to execute:
INSERT INTO translation (locale, model, foreign_key, field)
SELECT locale,model, foreign_key, 'company' FROM translation
WHERE model="Speaker" AND field="title";

cakephp - get table names and its column details

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());

Resources