Right number of element returned but no data inside with any find query

26 views
Skip to first unread message

Riccardo Coppola

unread,
Mar 11, 2012, 6:35:30 PM3/11/12
to yiimong...@googlegroups.com
Hi all,
I'm using the latest version of the extension (ymds-1.4-pr1).

I'm inserting data into mongo with success, there's no connection issue, but when I query the database with 
ModelClass::model()->findAll();
or
ModelClass::model()->find();

I get an array with the right number of elements, the right attributes, but no data:

array(5) (
  [0] => User object {
    CLASSNAME => (string) User
    password => null
    login => null
    first_name => null
    last_name => null
    email => null
    registerDate => null
    useCursor => null
    ensureIndexes => (bool) true
    _id => null
    _embedded => null
    _owner => null
  }
  [1] => User object {
    CLASSNAME => (string) User
    password => null
    login => null
    first_name => null
    last_name => null
    email => null
    registerDate => null
    useCursor => null
    ensureIndexes => (bool) true
    _id => null
    _embedded => null
    _owner => null
  }
...

The model is as follow:

class User extends EMongoDocument 
{
    public $password;
    public $login;
    public $first_name;
    public $last_name;
    public $email;
    public $registerDate;


    /**
     * This method have to be defined in every Model
     * @return string MongoDB collection name, witch will be used to store documents of this model
     */
    public function getCollectionName()
    {
        return 'users';
    }
    
    // We can define rules for fields, just like in normal CModel/CActiveRecord classes
    public function rules()
    {
        return array(
            array('login, email, password', 'required'),             
            array('password', 'validatePwd'),
            array('email', 'email'),
        );
    }
 
    // the same with attribute names
    public function attributeNames()
    {
        return array(
            'email' => 'E-Mail Address',
        );
    }
    
    /*
     */
    public function validatePwd($attribute,$params)
    {
        
        
    }

    /**
     * This method have to be defined in every model, like with normal CActiveRecord
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
}

I've followed the execution in debug mode for the ModelClass::model()->find(); call. I've found that the extension fetches the data correctly from mongo. 
In the EMongoDocument.php class, when it calls the setAttributes function (row 357) the $values array it receives is filled with the right data.
When the function then calls 
parent::setAttributes($values, $safeOnly);
and moves the execution to the CModel.php class, the attributes aren't set because the statement 

if(isset($attributes[$name]))  <-----
$this->$name=$value;
else if($safeOnly)
$this->onUnsafeAttribute($name,$value);

Always fails, so the returned array has the right attributes but are empty.
What can cause this?

Any help?
Thank you,
Riccardo

Attila Nagy

unread,
Mar 14, 2012, 9:59:56 AM3/14/12
to yiimong...@googlegroups.com
On Sun, Mar 11, 2012 at 11:35 PM, Riccardo Coppola <ricc...@gmail.com> wrote:
>     // the same with attribute names
>     public function attributeNames()
>     {
>         return array(
>             'email' => 'E-Mail Address',
>         );
>     }
>
> When the function then calls
> parent::setAttributes($values, $safeOnly);
> and moves the execution to the CModel.php class, the attributes aren't set
> because the statement
>
> if(isset($attributes[$name]))  <-----
> $this->$name=$value;
> else if($safeOnly)
> $this->onUnsafeAttribute($name,$value);
>
> Always fails, so the returned array has the right attributes but are empty.
> What can cause this?

The trouble is caused because you overridden the attributeNames()
method. I guess you've wanted to use the attributeLabels() instead.

--
Nagy Attila Gabor

Riccardo Coppola

unread,
Mar 14, 2012, 12:41:30 PM3/14/12
to yiimong...@googlegroups.com
Yes, it's true.
Now it works fine, thanks for your help.

Riccardo
Reply all
Reply to author
Forward
0 new messages