Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

find and findbycolumnname returning null

Hey helpers, My db model:

<?php
namespace Ocapp\Models;

use \Phalcon\Mvc\Model;

class UserHistories extends Model
{

    /**
     *
     * @var integer
     * @Primary
     * @Identity
     * @Column(type="integer", length=11, nullable=false)
     */
    public $id;

    /**
     *
     * @var integer
     * @Column(type="integer", length=11, nullable=false)
     */
    public $vendorId;

    /**
     *
     * @var integer
     * @Column(type="integer", length=11, nullable=false)
     */
    public $userId;

    /**
     *
     * @var string
     * @Column(type="string", nullable=false)
     */
    public $use_time;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->belongsTo('userId', __NAMESPACE__ . '\Users', 'id', [
            'alias' => 'user'
        ]);

        $this->belongsTo('vendorId', __NAMESPACE__ . '\Users', 'id', [
            'alias' => 'vendor'
        ]); 
    }

}

when i use

$userHistories = UserHistories::findFirst('userId=30');

it returns the resultset

but it returns null with find() and findByUserid().

No, it doesn't return resultset, it returns UserHistories class. If you use find() it should return resultset, even empty, same wiht findByUserId. Not sure, maybe find method doesn't accept condition as first argument?

Does find() return the correct record?

In my experience, you don't specify the column with findFirst, just the value. The column is automatically determined to be the primary key:

$userHistories = UserHistories::findFirst(30);

No find() also returning null. BTW, my phalcon version is 3.2.1 if it is of any help.

Does find() return the correct record?

In my experience, you don't specify the column with findFirst, just the value. The column is automatically determined to be the primary key:

$userHistories = UserHistories::findFirst(30);
edited Sep '17

i just get the following notice if i try to get the value of id (the primary key)

Notice: Undefined property: Phalcon\Mvc\Model\Resultset\Simple::$id in D:\xampp\htdocs\ocapp\app\modules\frontend\controllers\UsersController.php on line 288 NULL

if i use findFirst(1), then i get

string(1) "1"

It has taken my whole day but no clue till now.

No, it doesn't return resultset, it returns UserHistories class. If you use find() it should return resultset, even empty, same wiht findByUserId. Not sure, maybe find method doesn't accept condition as first argument?



114.4k
Accepted
answer
edited Sep '17

There is no chance that find returns null. Find returns RESULTSET class, even empty, it returns set of rows. You have id null beacause it's not your model yet., You need to do getFirst()->id.

You are trying to do:

Model::find()->id;

Which is wrong and will never work, beacause you have Resultset class. You need to do:

Model::find()->getFirst()->id;

Or if you need one record just:

Model::findFirst()->id;
edited Sep '17

I guess your issue after my last post is solved so i will mark it as answer.

yes, it has been resolved. just came back to mark it resolved but you already had done it...

Anyways, thanks.

I guess your issue after my last post is solved so i will mark it as answer.