Write to multiple tables in joomla component? - database

I'm trying to create a component (front end) that uses multiple tables. I found 1 or 2 post that partially answer to the question but none really does. The point seems always simple and evident for the one who knows how to do it but it is never really explained (or I missed the right post).
In my component, the user enters data in one view that need to be stored in two tables:
the standard Joomla User table i.e. # __users
an additional table to store data that are not included in Joomla i.e. # __users_complements
I'm a beginner, so maybe I'm wrong, but I understood that the standard functions of joomla can only save results of a form in one table .
In my case, I guess that I have to override the standard functions in my model: com_component / model / my_model.php.
1) I'm confused because I do not really understand which function must be overrided: save ()? store ()? other?
2) Let's say I override the save() function, should I rewrite all the code to save data (explode the data array and create all the update queries) or should I create 2 standard table objects.
In this case, (2 objects) it seems weird to send each time the whole data array to the parent function as I know that a part is for table 1 and the other part for the table 2. I should be able to split before don't I?
3) Should I create 2 models and manage those models from my controller when I get back data from the form and call the save function of the model?
Could you help me to clarify how to do this saving in multiple tables?
An example with code will be very much appreciated.
Thank you

I finally made it. As I spent many hours on this and found that a lot of people where looking for an answer, here is how I did.
I suppose you know how to create a component, using the standard MVC structure:
Component entry point
Component controller
Eventually component router
Component view
Component model
Component controller
In model components\my_component\models\my_model.php create your own save function
public function save($data)
{
// Initialise variables.
$userId = (!empty($data['id'])) ? $data['id'] : (int)$this->getState('user.id');
$user = JFactory::getUser();
$table_one = $this->getTable('TableOne', 'MyComponentTable', array());
$table_two = $this->getTable('TableTwo', 'MyComponentTable', array());
// Bind the data.
if (!$table_one->bind($data))
{
$this->setError(JText::sprintf('USERS PROFILE BIND FAILED', $user->getError()));
return false;
}
if (!$table_two->bind($data))
{
$this->setError(JText::sprintf('USERS PROFILE BIND FAILED', $user->getError()));
return false;
}
// Store the data.
if (!$table_one->save($data))
{
$this->setError($user->getError());
return false;
}
if (!$table_two->save($data))
{
$this->setError($user->getError());
return false;
}
return $user->id;
}
Of course, you need the getTable function called in the save function
public function getTable($type = 'TableOne', $prefix = 'MyComponentTable', $config = array())
{
// call the administrator\components\com_mycomponent\tables\__tablename__.php
$this->addTablePath(JPATH_COMPONENT_ADMINISTRATOR . '/tables');
return JTable::getInstance($type, $prefix, $config);
}
And it works! So simple!
Of course, as I said in my question the whole $data is sent to the parent save() function to with data that are not necessary for table_one or table_two. It works this way with the standard joomla structure (no hack or direct query in the code).
Hope it helps.

There may be those out there who disagree with the way that the following method disrupts the MVC structure just a bit, but I've found it to be the simplest for me.
Typically, you have a model that fits one of the tables. In your example with pushing data to the users table as well as one in your component, I would add the following to the model for the table in your component:
public function save($data) {
if (!parent::save($data)) {
return false;
}
// add necessary code to save to the users table, since there isn't a standard way to do this that I'm aware of
// sometimes I will grab another model even
require_once(JPATH_BASE . '/administrator/components/com_users/models/user.php');
$other_model = $this->getInstance('user', 'UsersModel');
$other_model->save($data);
return true;
}
The first part of this function should save the data to the components table just like normal. But you can tack what you need on to the rest of the component to make whatever you like happen.
I would almost guarantee that there is a better way to chain models (and I've seen some of the changes happening in the Joomla Platform core that will lead to better ways in the future), but this should get you going for now.
In addition, for prompt 3, I would handle in the controller if you need to sometimes save just one table and sometimes save both. I've found that the save functions are pretty safe to run even when parts aren't loaded, so I usually just let it run.

Related

CakePHP 3 Entity Confusion

Here is the issue I am facing all the time since I started to learn CakePHP 3
What is this concept of entity a real world example would help alot.
public function add()
{
// why do we have to create new entity / what is the role of entity here.
$comment = $this->Comments->newEntity();
if ($this->request->is('post','put')) {
// why do we have to use this line after posting / what is the role of this line.
$comment = $this->Comments->patchEntity($comment,$this->request->data);
if ($this->Comments->save($comment)) {
$this->Flash->success('comment submitted successfully.');
} else {
$this->Flash->error('Sorry, comment could not be updated.');
}
}
return $this->redirect($this->referer());
}
Let me open the book for you:
While Table Objects represent and provide access to a collection of
objects, entities represent individual rows or domain objects in your
application. Entities contain persistent properties and methods to
manipulate and access the data they contain.
-
why do we have to create new entity / what is the role of entity here.
Almost everything, if not all, in Cake3 works with entities, what an entity is is explained above. You need to create a new entity so that the FormHelper can work with it, AFAIR it can still work with an array if configured to do so as well but the entity should be used.
The reason entities exist is to abstract the data. Some people think entities are the representation of a DB row - that's wrong. As the book says, they can be a row but don't have to represent a row because the 3.0 ORM can work with other resources as well. In theory you can have a CSV data source that returns an entity per line.
I suggest you to read the entity code in the CakePHP core to get a deeper understanding of what else entities provide, just saying they're "just" a set of properties is to short thought.
why do we have to use this line after posting / what is the role of this line.
The post data is merged into the previously created entity, that's it. Use the API if you have basic questions like that. See the API entry for patchEntity().
In simple word, Entity is a set of one record of table and their relational table, on that you can perform operation without touch of database and encapsulate property of entity (fields of table) as you want.
Advantages of Entity.
Modifying result sets outside of the database (for formatting or otherwise)
Needing to represent both the table and row in the same class.
Data validation was a fucking nightmare.
Inconsistent API in terms of both how we handled things internally as well as what (and how) we returned stuff.
Other random stuff as you want.
You can do run-time modification of result sets. Just add a method to your entity to return results in the way you want. This also means you can use composition for managing entities (yaya traits)
Validation is beautiful. We can validate data before it gets into an object and then validate the object state in a separate step.
It is easier for developers to understand what they are dealing with. You either have an object or an array of objects. An object can be linked to data which can also include other objects, but you no longer have to guess at what the array key will be, nor whether its nested funkily.
We can iterate on the interface for tables and entities separately. We couldn't easily change internals for the old Model class because of the implications on both, whereas now we can (in theory) change one without mucking about in the other.
It looks prettier simple.
Try this:
if ($this->request->is('post','put')) {
$data = $this->request->getData();
$comment = $this->Comments->newEntity();
$comment = $this->Comments->patchEntity($comment, $data);
$status = $this->Comments->save($comment);
if ($status) {
$this->Flash->success('comment submitted successfully.');
} else {
$this->Flash->error('Sorry, comment could not be updated.');
}
}
return $this->redirect($this->referer());
}
My advice is never use Post and Put in the same function. Just for good pratice. Put works fine when you make a update using id like a parameter.

CakePHP actsAs Translate and $Model::find()

I have attached the Translate behavior to one of my models and I have some shortcomings regarding this:
1) If I don't save data in all fields passed as params when attaching the behavior to the model, $Model::find() method doesn't get the inserted rows.
public $actsAs = array(
'Translate' => array(
'title' => 'title_Translation',
'description' => 'description_Translation',
'description_long' => 'description_long_Translation'
)
);
Ex: if i pass to $Model::save() method only a value for 'title', the data is saved, even in the i18n table, but the $Model::find() doesn't get anything. I must pass data for all the fields.
Can I force it to retrieve those records ?
2) How can I get all the records in the admin side of the application (regardless of the language in which a record is saved) in order to list them so the user can alter it (edit data, save data in multiple languages)? Right now, I can only get the records that correspond to the current language (read from Configure or set explicitly)..
Thank you!
I kind of solved it, I copied the TranslateBehavior to app/Model/Behavior (just to avoid problems on future upgrades and keep the original one just in case) then I changed the _addJoin(...) method of the behavior, just changed the join type from INNER to LEFT on line 255 (I use cake 2.2.3).
Now if a record exist it is always retrieved, even if translated fields are missing.
Don't see any drawbacks besides the need to check if the translation field is empty.
OK, I might be a bit late, but anyway...
1) Cake uses an INNER JOIN when fetching a row and it's associated translations, so basically there's no easy way around this. You have to make sure you save every translatable field, every time - even if you just save it as blank. The only alternative would be to go hacking round the core to make it use a left join rather than an inner join - but don't do that.
2) The cookbook explains how to fetch all records here: http://book.cakephp.org/2.0/en/core-libraries/behaviors/translate.html#retrieve-all-translation-records-for-a-field
Now, probably most of the time you want to get just one translation, so you don't want to modify the definition of your $actsAs['Translate'] array in your model. So what I did, was set up a method in AppModel.php which modifies the $actsAs['Translate'] array on the fly:
/*
* See http://book.cakephp.org/2.0/en/core-libraries/behaviors/translate.html#using-the-bindtranslation-method
* This is for making it so we fetch all translations, as opposed to just that of the current locale.
* Used for eg. editing (multiple translations) via the admin interface.
*/
public function bindAllTranslations(){
$translatableFields = $this->actsAs['Translate'];
$keyValueFields = array();
foreach($translatableFields as $field){
$keyValueFields[$field] = $field.'Translation';
}
$this->bindTranslation($keyValueFields,false); // false means it will be changed for all future DB transactions in this page request - and won't be reset after the next transaction.
}
So, if it's an admin method (or any other situation you want all translations) you call that code before doing a find:
$this->MyModel->bindAllTranslations();
$this->MyModel->find('all');
Hope that helps!
Not exactly sure if it will help in your case, but you can also use
array to set locale before you call find()
$this->YourModel->locale = array("ENG", "GER", "JAP");
This way you will always get all records even if they don't have all possible translations.
Thanks a lot eleonzx, I'm having this problem since a decade, and with your simple answer I can now move forward ! So thanks again.
And maybe this code can help a lot of people :
in my AppController beforeFilter method I call _setLanguage
private function _setLanguage() {
if($this->Session->read('Config.language')){
$locale = $this->Session->read('Config.language');
$this->{$this->modelClass}->setLocale($locale);
}else{
$this->{$this->modelClass}->Behaviors->disable('Translate');
}
}
With the else condition I disable the Translate Behavior on the fly to get the original contents if there is no locale set in the session (I use basic links to switch between languages).

Can I deal with Records in an easier way in CakePHP?

I started to tackle CakePHP and read through the documentation, but two things still seem a bit clumsy to me.
I know other Frameworks where I have a certain record which I'd like to store, but CakePHP suggests me to do it anonymously:
$this->Foo->create(array(...));
$this->Foo->save();
Why can't I tell CakePHP which Record to save, just like in every other framework:
$foo = $this->Foo->create(array(...));
$foo->save();
I would like to iterate through a whole RecordSet inside of a Controller. Why do I need to iterate using
$foos = $this->Foos->find('all');
foreach($foos as $foo){
$foo['Foo'] // ... here we have $foo.
I don't understand why find() returns a 2-dimensional array and there are only records in the inner array. Why isn't this directly an array of records?
$this->Foo is an instance of your Foo model. When you call methods on it, you are calling methods on the active record (if there is one) of that instance of the Foo model. So in terms of telling Cake which record to save, you don't need to - Cake knows to save the current active record.
Here's the code you pasted with comments, which might help.
// Prepare this instance of the Foo model to save a new record
$this->Foo->create(array(...));
// Save the new record that we have just prepared
$this->Foo->save();
And the other way...
// Call the create method on this instance of the Foo model, and return what?
// Return another instance of the Foo model?
// Why not just continue using the instance we already have, ie, $this->Foo
$foo = $this->Foo->create(array(...));
// Call the save method on the duplicate instance of the Foo model that was
// returned from the create method?
$foo->save();
// Why did 'create' need to return a duplicate instance of the model to do a save???
// Why not call the save on the same instance of the Foo model that we used to call the create?
Point 2. This is basically for consistency. Often, you'll be returning data from multiple tables, linked to one another. Lets say tables Foo and Bar have a 1 to 1 relationship, and you're getting Foo records, along with their associated Bar records.The array returned will need Foo and Bar keys, eg: inside of your foreach loop, $foo might contain:
$foo['Foo']['column1'], $foo['Foo']['column2'], $foo['Bar']['column1'], $foo['Bar']['column2']
To be consistent, when you only fetch from one table, it still returns in the form $foo['Foo']['column1'], just like it would if you fetched joined data from multiple tables.
EDIT: In response to your comment, say you have the code:
$foos = $this->Foos->find('all');
Say you wanted to call some model method on each row of your returned array, there are a few ways you could do it. One way is something like:
// This is code for the controller
$this->Car->find('all');
foreach($cars as $car){
$this->Car->driveTwoMiles($car); // the driveTwoMiles would be in your model class
}
So in your model, you'd have a method:
// This would be a method in your model class
function driveTwoMiles($car){
$this->id = $car['Car']['id']; // set the active record
// we are now inside the model, so $this->id is the same as calling $this->Car->id from the controller
// Do whatever you want here. You have an active record, and your $car variable, holding data
$this->Post->saveField('distance_driven', $car['Car']['distance_driven']+2);
}
Also, for cases where you just want to update one record, not many, you can just do a "read" rather than a "find('all')" - more info in the links below.
I would highly recommend reading all the way through these pages in the cake cook book:
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html - Retrieving Data
http://book.cakephp.org/2.0/en/models/saving-your-data.html - Saving data
http://book.cakephp.org/2.0/en/models/deleting-data.html - Deleting data
All contain really important foundational info on how to work with Cake Models. Spend the time to really understand it now, and you'll save yourself countless headaches and code re-factors in future!

cakephp - what is the difference between model and behavior?

I understand that the behavior is supposed to extend the model and add functionality to it, but in most cases the fat-model idea is making the behavior useless, isn't it?
And, even preferred, ignore my first paragraph and just answer - please - the question in the title, and add an example to make it clear
thanks
A behavior is where you extract code that doesn't really belong in one specific models domain. Kind of like, helper functions, or a mixin/module (if you're familiar with that pattern from other programming languages).
If you're familiar with CakePHP helpers and components, you can look at it like this. A behavior is to Model as Helper is to View as Component is to Controller. Basically a set of functionality that will be used across multiple models.
Let's say you want to implement soft delete on all the models in your application. (Soft delete meaning, you don't actually delete the record, you just mark it as deleted). You wouldn't want to put the same soft delete code into every model. That's not very DRY! Instead you use a behavior to abstract out the functionality like so.
What we're trying to do is instead of delete the record, update the deleted on column with the current date (it will work the same way as created, modified). Then we'll change the find method to only retrieve records that aren't deleted.
// models/behaviors/soft_deletable.php
class SoftDeletableBehavior extends ModelBehavior {
function setup(&$Model, $settings = array()) {
// do any setup here
}
// override the delete function (behavior methods that override model methods take precedence)
function delete(&$Model, $id = null) {
$Model->id = $id;
// save the deleted field with current date-time
if ($Model->saveField('deleted', date('Y-m-d H:i:s'))) {
return true;
}
return false;
}
function beforeFind(&$Model, $query) {
// only include records that have null deleted columns
$query['conditions']["{$Model->alias}.deleted <>"] = '';
return $query;
}
}
Then in your model
Class User extends AppModel {
public $actsAs = array('SoftDeletable');
}
And from your controller, you can call all of our behavior methods on your model
Class UsersControllers extends AppController {
function someFunction() {
$this->User->delete(1); // soft deletes user with id of 1
$this->User->find('all'); // this will not exclude user with an id of 1
}
}
I hope this helps you.
Behaviors can be shared between Models. Typically a Behavior contains abstract code that can be applied to any model.
While you could of course write this for a single Model specifically, you would have to write it again for another Model. By abstracting it to be shared, you've created a Behavior.
In CakePHP a Behavior to a Model is the same relationship as a Component to a Controller or a Helper to a View.
An example of a Core Behavior in CakePHP is Containable. This allows you to have finer control over the relationships used in by find().
basically behaviors are used to make your application dry! and code reuse...
Check this link... it gives you simple tagging behavior which you can use in your post model

How do I associate a database table with a model in ATK?

I have legacy code which stores temporary data in the context. I would like to store this in the DB using the following model:
class Model_MyModel extends Model_Table {
function init(){
parent::init();
$this->addField('myString');
}
}
I can access the data from within the legacy Controller thus:
class Controller_LegacyController extends Controller {
$myString = $this->api->recall("legacyString");
}
But I can't see how to tie everything together (all the examples use a Form to link to the DB)
Thanks for your help,
Greg.
I find your question and code a bit confusing, but I'll try to help.
You don't need controller to be able to use your model. When calling $form->setModel() it automatically pick the right controller for you.
$page->add('MVCForm')->setModel('MyModel');
When you want to send data back into data-base, you should call $form->update(). There is a View you can use, which will do that for you called: FormAndSave
$page->add('FormAndSave')->setModel('MyModel'); // will also save data back to database.
If you load data from database, you need to call loadData() on the model. Your final code might look like this (stickyGET ensures that it pass get argument inside form submit handler):
$this->api->stickyGET('id');
$page->add('FormAndSave')->setModel('MyModel')->loadData($_GET['id']);
method recall() deals with sessions, so it seems as if you are reading data from the session. If you intend that and you want to see value of your session variable in the form, then this will do it:
$form->set('myfield',$this->api->recall('legacyString'));
I hope this will give you some hints on how to continue. Look through more samples, there are lots of them on http://agiletoolkit.org

Resources