Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
many_to_many relationship error: Object of class ___ could not be converted to string
  • Hello all,

    I'm currently creating trying to create a relationship between two models but am facing the following error. It's strange because I've done this many times before without issue. Could it be related to namespaces?

    Fuel\Core\PhpErrorException [ Runtime Recoverable error ]:
    Object of class Language\Model_language could not be converted to string

    In my model I have the following

    <?php
    namespace User;

    class Model_user extends \Orm\Model_temporal{

    protected static $_many_many = array('languages'=>['model_to'=>'\Language\Model_language']);

    }


    And the code I am trying to use is 
    $language = \Language\Model_language::find($language_id);
    $user->languages[]=$language;
    $user->save()

    I also tried to use
    $language = \Language\Model_language::find($language_id);
    $user->languages[$language->id]=$language;
    $user->save()



  • And how is the other side of the relation defined?
  • <?php
    Namespace Language;

    Class Model_language extends \Orm\Model_temporal{
    protected static $_many_many = array('users'=>['model_to'=>'\User\Model_user']);

    }
  • I see two possible issues:

    Your classes are named incorrect. There should be a capital after the underscore, so Model_Language, and Model_User.

    And when specifying relations, you should not use a leading backslash when specifying fully namespaced classnames in strings, so "User\Model_User" and "Language\Model_Language".

    Also, if User and Language are modules or packages, they should be loaded before the class can be used.

    You didn't mention the Fuel version you are using, but it's always good to switch (temporarily) to 1.8/develop to see if you're experiencing a bug that is already solved.
  • Is this correctly set up?
  • Sorry I didn't see that reply. I am using 1.7.

    When I switch to /ORM/Model rather than Orm/Model_temporal the error doesn't throw any more.

    I will try your above suggestions thank you!
  • Implemented your above suggestion and it still throws the same error. I will try using 1.8dev.
  • If you still have the issue in 1.8/dev, please create an issue for it at https://github.com/fuel/orm/issues with a documented use case and code samples.

Howdy, Stranger!

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

In this Discussion