Load database table in controller cakephp - database

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.

Related

Cakephp 3 : Without use joining is it possible load another model in a model?

In controller I can use loadModel method to get another table data in cakephp.But is it possible in model? For example I have used ORM/Table class. I defined my table name used table method.Like below code
$this->table('blogs');
Is it possible declare another table in same way without association ?
Note : I am able to do it in controller.
If you want to load a model in another table you can use TableRegistry Reference
use Cake\ORM\TableRegistry
$otherTable = TableRegistry::get('other');
//do something with the other table
//example
$someRecord = $otherTable->find()->first();
If your model is associated with the other model that you want to load via an association (belongsTo, hasOne etc.) then you can directly use it like this for example:
$this->otherModel->find()->contain()->where(['someCondition' => 'value'])->first()
Having said that, you really cannot define two tables into one if that is what you are trying to do. You can load and use a different table inside another table, but you can't define two tables under one table class.
I don't really understand why you don't want to define an association here as it sounds exactly like what you should do, but I hope the table registry option covers you

CakePHP model association based on field values?

I've got three tables (there's actually several more, but I only need the three for this problem). Applications, Appattrs and Appcats. In CakePHP parlance (as best as I can since I'm still learning the framenwork) Applications hasMany Appattrs and Appattrs belongsTo Applications. Easy.
The problem comes when I want to associate Appattrs and Appcat - the association is predicated on a field value and a corresponding foreign key in Appattrs. For instance:
If appattrs.type = 'appcatid' then appattrs.value would point to a record in the Appcat table.
The appattrs table holds static data appattrs.type='dateadded' and value='201201011300' as well as foreign key references. I'd rather not get into a discussion as to why data is stored this way, I just want to figure out how to create associations that will let me pull an application record, the associated attr records and then an attr record with its associated row from the appropriate table. Dynamically.
It seems to me that I should be able to create a model based on a query and then associate that model - I just can't seem to figure out how to do that.
--
If I need to post schema for the three tables, I can. I can also post my current model code, but honestly, right now it's just association variables so I don't think it'll get anyone anywhere.
Thow I do not understand the logic behind this design, I thing what you are looking for
is Creating and Destroying associations on the fly.
On this section of CakePHP Docs, it describes how you can associate models from within the corresponding controller.
So, for example, when you want to save specific data to Appattr model you can do some data checking and create your association using bind() method.
A very abstract approach to the above would be something like this
public function yourmethod() {
...
if ($this->request->data['Appattr']['type'] == 'sometype') {
$this->Appattr->bindModel(
array(/*Your association*/ => array(/* Your attributes...*/)
);
/* Rest of the logic follows */
}
}
This way you get your job done, but it's very possible to end up having very complicated
data in your database and thus having very complicated code.
I hope this helps

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

Cake PHP revisioning data/id refresh

So here is what I am trying to do.
I have one form which will post data to two tables say table a and table b. Now when I edit the same record I want that data in one table should be updated and in other table it should create new row and add the data.
Can anyone tell me how to achieve this in cakephp
Thanks,
When saving, if the primary key (usually id) of the model is given, cakephp will update, otherwise it will insert.

Order data using values stored in another table

I've been using cakephp for a while, but have not learned all the ins and outs yet so I may be missing something simple. Or the problem may lie with my database structure. Either way, if anyone has any idea of what I'm doing wrong, please share.
Is there a way to order the data returned by cakephp's find using values stored in another table?
I am creating custom form fields on a per category basis, so when I choose a particular category to post in, custom fields will be added to my form. I have 3 tables: Posts, Fields, and Answers. The Posts table stores the basic static information for the post, such as id, category_id, title, and description. The Fields table stores the custom field data, such as category_id, field_label, field size, etc. The Answers table stores the values that are entered for particular fields, such as post_id, field_id, value.
I am trying to display the posts for a particular category, and create html table headers on the fly, using select fields, set by a column toggle in the fields table, and also select the answers associated with that particular field and post.
I am able to select all the data I want, and paginate everything just fine, but what I can't seem to figure out is how to order the data using one of the dynamic column values. For example, if I have year, make, and model as 3 custom fields, I would like to click the year column to sort my results by the year values, and if I click the make column, I would like to sort my results by the make values, etc.
I know how to order the results by a particular field inside the posts table, such as id or title, but is it possible to order using the custom fields? Am I setting up the database and/or something else wrong, and if not is there are particular cakephp method or sql command that I need to use in order to sort by the custom fields? I'm not really well versed in complex sql commands.
Thanks.
I'd suggest you pass the field name and sort direction in the URL (GET param). So when you have your table header link, form it so that it links to a URL as so:
http://somesite.com/pages/index/sort:customfield1/dir:asc
Then when you're grabbing the data from the db in your find() query, include the named parameters as the order parameter that can be sent to find.
You'll need to determine a default sorting column and direction. Maybe have that be selectable with a boolean field in the schema -- if there are no parameters sent to the action above, pull the field from your other table that has default set to true in the record.
To clarify: when a user visits a given action, first you'll pull the custom fields from the other table. Then using those fields (either the default as mentioned above, or the named params passed in the URL) form the query for the actual data, using the order parameter.

Resources