Welcome, Guest
Username: Password: Remember me

TOPIC: Saving Item data to another Table/Model

Saving Item data to another Table/Model 26 Oct 2015 17:19 #13609

  • liubov
  • liubov's Avatar
  • Offline
  • Elite Member
  • (=) 10 mn and it's ready!
  • Posts: 279
  • Thank you received: 36
  • Karma: 22
Hi Cookers,

Some users ask me for adding a new component functionnality.

Pedigrees is my main Table. Full details of a dog - name, father_id, mother_id, and more ...
Etalons is the Males Table
Lices is the Females Table


When I save a new Etalon item, I also want to push his name $data["fname"] in the main table as a new Pedigree item, in dname field.

Can we save an Item field cross differents models ?
How and where (models/controllers) to write properly this custom code ?

Thank for help
The administrator has disabled public write access.

Saving Item data to another Table/Model 27 Oct 2015 09:20 #13614

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
Hi liubov,

Sure you can, but you have to use the fork system. Are you familiar with that?

Fork the etalon model (singular), then copy the "save($data)" function from the original.

In the save function, add an instance of the pedigree (singular) model

Something like
		$pmodel = CkJModel::getInstance('pedigree', 'JBreedingModel');				
		$result = $pmodel ->save(
			array(
				'dname' => $data['fname']
			)
		);
The administrator has disabled public write access.
The following user(s) said Thank You: admin

Saving Item data to another Table/Model 27 Oct 2015 11:00 #13615

  • liubov
  • liubov's Avatar
  • Offline
  • Elite Member
  • (=) 10 mn and it's ready!
  • Posts: 279
  • Thank you received: 36
  • Karma: 22
Thanks a lot Romkabouter.
Yes I do. The fork system is to write custom code and you can continue to update main code with Jcook Builder.

But if I do not update my component with the Builder, can I write this code directly in the original architecture ? there are no objection to do that ?
The administrator has disabled public write access.

Saving Item data to another Table/Model 27 Oct 2015 11:34 #13616

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
Yes, you can write it in the original code.

But that way your component will be more difficult to maintain.
You already have users asking for more functionality, so it is possible that in the future you have to regenerate your component ;)

In the end it is your choice and yours alone :)
The administrator has disabled public write access.
The following user(s) said Thank You: liubov

Saving Item data to another Table/Model 27 Oct 2015 12:54 #13621

  • liubov
  • liubov's Avatar
  • Offline
  • Elite Member
  • (=) 10 mn and it's ready!
  • Posts: 279
  • Thank you received: 36
  • Karma: 22
so simple and work fine !
I was on a bad way, trying throught controllers ...
Method understood !

Thank you again for your quick answers.
The administrator has disabled public write access.

Saving Item data to another Table/Model 27 Oct 2015 12:58 #13622

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
Well, if you are a purist, you should fork the controller and the save function in there ;)

My solution is not pure MVC, because there is businesslogic in the model, and that should be in the controller.
The administrator has disabled public write access.

Saving Item data to another Table/Model 27 Oct 2015 13:08 #13623

  • liubov
  • liubov's Avatar
  • Offline
  • Elite Member
  • (=) 10 mn and it's ready!
  • Posts: 279
  • Thank you received: 36
  • Karma: 22
it is exactly what i 've done at final (following your Fork advice)...
The administrator has disabled public write access.

Saving Item data to another Table/Model 27 Oct 2015 13:59 #13631

  • MorganL
  • MorganL's Avatar
  • Offline
  • Platinum Member
  • Posts: 438
  • Thank you received: 53
  • Karma: 15
I tried to use this method for something I coded recently, but the save it just not firing at all.

If I take a vanilla save process... i.e
public function save($data)
	{
		if (parent::save($data)) {

			return true;
		}
		return false;

	}

Logically I would put the extra model code before the return true; statement

i.e
public function save($data)
	{
	
		if (parent::save($data)) {

// Put code here


			return true;
		}
		return false;


	}

But nothing happens (currently I am using the traditional Joomla database query in this spot and it works fine, but your method looks sooooo much sweeter)

My attempt is
public function save($data)
	{
	
		if (parent::save($data)) {

                        $bemodel = CkJModel::getInstance('changelog', 'MycomponentModel');
                        $result = $bemodel ->save(
                            array(
                                'genre' => $data['id'],
                                'notes' => 'Genre updated'
                            )
                        );


			return true;
		}
		return false;


	}
Morgan Leecy MCSE

Novell / Linux
PHP. MYSQL, Apache, node.js
Coldfusion, JQuery, HTML5
Joomla
Last Edit: 27 Oct 2015 13:59 by MorganL.
The administrator has disabled public write access.

Saving Item data to another Table/Model 27 Oct 2015 14:07 #13633

  • MorganL
  • MorganL's Avatar
  • Offline
  • Platinum Member
  • Posts: 438
  • Thank you received: 53
  • Karma: 15
Forget that, just redid it and was calling the model for the COLLETION not the ITEM... bah!
Morgan Leecy MCSE

Novell / Linux
PHP. MYSQL, Apache, node.js
Coldfusion, JQuery, HTML5
Joomla
The administrator has disabled public write access.
Time to create page: 0.068 seconds

Get Started