Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Is it possible to clone object's values only in FuelPHP ?
  • Hi there,

    Is there a fastest way to clone a modal_object's property to another model_object in FuelPHP ORM rather than looping and equating each properties ?

    This might not be a professional way to copy complete row , but I want to know whether this type of action is possible or not.

    e.g. I need an employee row to be inserted in users table (both object have same properties ).
    I did this :

    $employee = Model_Employee::find('first');
    $user = clone $employee;
    $user->id = null;
    $user->save();

    /* this action insert a row in employees table but i need it to be inserted in users table */
    Thanks,
    Ishwor
  • If you clone a model object, it remains the same model object, so that will never work.

    You could try

    $employee = Model_Employee::find('first');
    $user = Model_User::forge($employee);
    $user->id = null;
    $user->save();
  • Hi Harro ,

    Thanks for the response. But I'm sorry I'd tried that too and got a runtime recoverable error like :
    it expects array as parameter to forge not an object
    Furthermore,
    I tried 
    $user = Model_User::forge((array)$employee);
    too,
    It tries to insert an empty values in the user's properties.

    so what I did is 
    Model_User::forge(array( 
    'property1'=>$employee->property1,
    'property2'=>$employee->property2,
    & so on 
    ));

    Is this because employee and user has different relations ?

    I wish to know if there's something better solution regardless their relations. I wish to copy just the properties because this might be tedious if there are large no. of properties.

    Thanks,
    Ishwor
  • If you just do a find like in your original post, relations aren't loaded up front, they are loaded on demand.

    Can you tell me exactly what error you got, and on which file/line? An ORM model implements ArrayAccess, so if my suggestion gives an error, there must be a bug somewhere.

    Alternatively, you can use:

    $user = Model_User::forge($employee->to_array());

    Casting to an array won't work, as the model data is not stored in individual properties.
  • Hi Harro,

    Sorry for using type casting but I tried that method too.

    public function action_synchronize()
    {
    $employee = Model_Employee::find(1);
    $user = Model_User::forge($employee->to_array());
    $user->id = 100;
    $user->joined_date = time();
    $user->save();
    Debug::dump($user);exit;
    }

    I need to insert a new id in users table so changing id is not allowed here.
    It expected to assign each property individually.
    I've attached the screenshot herewith.
    I'm pleased with your quick response again.

    Sorry Harro , Is that only me who couldn't know how to upload images directly ? I think you can view this link.


    Thankfully,
    Ishwor
  • HarroHarro
    Accepted Answer
    The original issue has been addressed in 1.9/develop, you can pass objects inplementing ArrayAccess to forge() now.

    No more need for the exact error message.
  • Oh !
    Thanks Harro !
    I'm waiting eagerly for the new version !

    Hopefully,
    Ishwor
  • HarroHarro
    Accepted Answer
    Will probably be released some as a 1.8 hotfix, so all you have to do then is run a composer update.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion