Tony Evyght
Tony Evyght

Reputation: 2575

Copy object to other table - Symfony 1.4

I have two table:

First:
id | name  | password
 1 | peter | old

Second:
id | name  | password

I get object from table First:

$first = Doctrine::getTable('First')->find(1);

$copy = $first->copy();

$second = new Second($first);
$second->save();

or

$second = new Second($copy);
$second->save();

In both cases i have:

Second:
id | name  | password
 1 | NULL  | NULL 
 2 | NULL  | NULL

Is possible to make this copy?

Upvotes: 1

Views: 1536

Answers (4)

Ghazaleh Javaheri
Ghazaleh Javaheri

Reputation: 2117

From documentation peer classes are:

classes that contain static methods to operate on the tables. They provide a way to retrieve records from the tables. Their methods usually return an object or a collection of objects of the related object class

and model classes represent a row in a table and exist for representing a row of table

Upvotes: 0

Jestep
Jestep

Reputation: 985

Why wouldn't you use clone? It's less cumbersome than using toArray, fromArray.

$first = Doctrine::getTable('First')->find(1);
//do whatever to $first here...

$second = clone $first;
$second->save();

You may have to set the ID field on $second to null though.

Upvotes: 2

j0k
j0k

Reputation: 22756

Have you tried with toArray / fromArray ?

$first = Doctrine::getTable('First')->find(1);

$second = new Second();
$second->fromArray($first->toArray());
$second->save();

Upvotes: 3

Dario Pedol
Dario Pedol

Reputation: 2110

Sure, but I don't think like this. Where did you see that? I don't think you can pass one entity as a parameter to the constructor of another.

Just do it manually or use reflection to copy all the fields:

$first = Doctrine::getTable('First')->find(1);

$second = new Second();
$second->setValue1($first->getValue1());
$second->setValue2($first->getValue2());
...
$second->save();

Upvotes: 1

Related Questions