We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Can not use getFirst() after fetch from Phalcon\Mvc\Model resultset

I try to do this


$presale = \Pkerp\Models\Sale::findFirst( $sdata['saleId'] );

$nowsale = \Pkerp\Models\Sale::find("type={$this->request->getPost('type')} AND CustomerId={$this->request->getPost('customerId')}")
                            ->filter(function($row){
                            if( $row->getStates(array("EmployeeId={$this->session->get('auth-identity')->employeeId}",
                                           'order'=>'setTime DESC'))->getFirst()->statecode == \Pkerp\Models\SaleState::STAT_INIT ){
                            return $row;
                            }
                });

if( count($nowsale) > 0 ){
$sale = $nowsale->getFirst();
}

But I got this error .

Fatal error: Call to a member function getFirst() on a non-object in ...



58.4k

Hi

I think you have use >getFirst() method in Filtering Resultsets, so I think it has been return the first record

edited Jul '15

I think there is a problem on your namespaces definiton on models, review your model Sale, must include :

namespace \Pkerp\Models;


4.0k

At first,thanks for reply :)

I found out I can access by ths way

    if( count($nowsale) > 0 ){
                    $sale = $nowsale[0];
                    .....
    }

So I think the keypoint is it's transfer to another class form \Phalcon\Mvc\Model\Resultset\Simple to something maybe?

But the $nowsale[0] still keep object type like..


object(Pkerp\Models\Sale)[406]
  public 'saleId' => string '58' (length=2)
  public 'CustomerId' => string '1' (length=1)
  public 'type' => string '0' (length=1)
  public 'formatpath' => null
  public 'showname' => string '陳建中問貨紀錄表' (length=24)
  public 'totalPrice' => string '0.00' (length=4)
  public 'totalGoods' => string '1' (length=1)
  public 'numorder' => string '1040719SR007' (length=12)
  public 'showdate' => string '2015-07-20' (length=10)
  public 'comments' => null
  public 'payway' => string 'month' (length=5)
  public 'paydate' => string '2015-07-31' (length=10)
  public 'lastrecord' => string '1' (length=1)
  public 'showdate_tw' => string '104/07/20' (length=9)
  public 'paydate_tw' => string '104/07/31' (length=9)
  protected 'typeLabels' => 
    array (size=4)
      0 => string '問貨紀錄表' (length=15)
      1 => string '補單紀錄表' (length=15)
      2 => string '客戶日銷貨單' (length=18)
      3 => string '銷貨退回紀錄表' (length=21)
  protected '_dependencyInjector' => 
    object(Phalcon\Di\FactoryDefault)[2]
      protected '_services' => 
        array (size=29)
          'router' => 
            object(Phalcon\Di\Service)[42]
              protected '_name' => string 'router' (length=6)
              protected '_definition' => 

it was what I want to fetched by $nowsale->getFirst()...

maybe code was fine but I want to know this reason?

I think you have use >getFirst() method in Filtering Resultsets, so I think it has been return the first record

at that code section I use getFirst() from relation model \Pkerp\Models\SaleState from \Pkerp\Models\Sale but I want to getFirst() from \Pkerp\Models\Sale outside