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.

Record to object

I can't retrieve documents using finFIrst. I use the primary key compsed of three elements from my table of eight rows. This is the code:

public function findAction($establishment_id, $city, $district)    {
        echo "here";

        $branch= Branch::findFirst(
            [
                'columns'=> '*',
                'conditions' => 'establishment_id= ?1 AND city=?2 AND district= ?3',
                'bind' =>[
                    1=>$establishment_id->establishment_id,
                    2=>$city->city,
                    3=>$district->district
                ]
            ]
        );

        if ($branch === false) {
            $this->response->setJsonContent(
                [
                    "status" => "NOT-FOUND"
                ]
            );
        } else {
            $this->response->setJsonContent(
                [
                    "status" => "FOUND",
                    "data"   => [
                        "establishment_id"   => $branch->establishment_id,
                        "city" => $branch->city,
                        "district"=>$branch->district
                    ]
                ]
            );
        }

        return $this->response;
    }
When I comment the lines of 'bind' the echo above shows, but when I uncomment it  I have false. What could be the issue ?

can u make sure the values of these $establishmentid->establishmentid $city->city $district->district

you can also use something like this

$branch= Branch::findFirst(
    [
        'conditions' => 'establishment_id= :establishment: AND city=:city: AND district= :district:',
        'bind' =>[
            'establishment' => $establishment_id->establishment_id,
            'city' => $city->city,
            'district' => $district->district
        ]
    ]
);

you can also check bindTypes

"columns"=>"*" is not needed. By default it will select whole object. You have false because nothing is returned.

Nothing is returned and that's the problem , I have some records i'm want to retrieve

"columns"=>"*" is not needed. By default it will select whole object. You have false because nothing is returned.

What do you mean by bindType ?

can u make sure the values of these $establishmentid->establishmentid $city->city $district->district

you can also use something like this

$branch= Branch::findFirst( [ 'conditions' => 'establishmentid= :establishment: AND city=:city: AND district= :district:', 'bind' =>[ 'establishment' => $establishmentid->establishment_id, 'city' => $city->city, 'district' => $district->district ] ] );

you can also check bindTypes

And also I have getters/setters could they cause a problem ? I don't really understand want type of parameters does bind take

Use the solution mentionned before event if yours should be work.
Then the binType can be use to check the type of your variables which are binded see : https://docs.phalconphp.com/uk/latest/reference/models.html#binding-parameters
And you need to check if the variables binded are really set.
Other way you can use query builder and show the query builded to know why that not work.
https://docs.phalconphp.com/en/3.0.2/api/Phalcon%5Mvc%5Model%5Query%5Builder.html

bindType is to make the query more efficient http://php.net/manual/en/mysqli-stmt.bind-param.php

What do you mean by bindType ?

can u make sure the values of these $establishmentid->establishmentid $city->city $district->district

you can also use something like this

$branch= Branch::findFirst( [ 'conditions' => 'establishmentid= :establishment: AND city=:city: AND district= :district:', 'bind' =>[ 'establishment' => $establishmentid->establishment_id, 'city' => $city->city, 'district' => $district->district ] ] );

you can also check bindTypes

also since you have updated the code, public function findAction($establishmentid, $city, $district) { vardump( $establishment_id, #are these objects or values? $city, $district );

Nothing is returned because there are no existing records for conditions you provided. Just use such dblistener:

class DbListener
{

    /**
     * DbListener constructor.
     */
    public function __construct()
    {
        $this->config = Di::getDefault()->get('config');
    }

    public function beforeQuery(Event $event, Pdo $connection)
    {
        $logger = new Phalcon\Logger\Adapter\File(
            APPLICATION_PATH.'/'.$this->config->application->logsDir."/sql-debug-"
            .date('Y-m-d').".log"
        );
       $logger->info(
                "Query: ".$connection->getRealSQLStatement().PHP_EOL."Params: "
                .json_encode($connection->getSqlVariables()),
        );
    }
}

And in your db service:

...
 $eventsManager = new Manager();
        //Listen all the database events
        $eventsManager->attach('db', new DbListener());
        //Assign the eventsManager to the db adapter instance
        $adapter->setEventsManager($eventsManager);

        return $adapter;

they shoud be values , not object

bindType is to make the query more efficient http://php.net/manual/en/mysqli-stmt.bind-param.php

What do you mean by bindType ?

can u make sure the values of these $establishmentid->establishmentid $city->city $district->district

you can also use something like this

$branch= Branch::findFirst( [ 'conditions' => 'establishmentid= :establishment: AND city=:city: AND district= :district:', 'bind' =>[ 'establishment' => $establishmentid->establishment_id, 'city' => $city->city, 'district' => $district->district ] ] );

you can also check bindTypes

also since you have updated the code, public function findAction($establishmentid, $city, $district) { vardump( $establishment_id, #are these objects or values? $city, $district );



1.7k
Accepted
answer

you are using it as an object...

    $establishment_id->establishment_id,
    $city->city,
    $district->district

Thank you it helped, a lot. The last question and I promise I'll close the thread... I have three binded object, but I aim to retrive as one, I did it that way because those field are setted as the primary key. How can I retrieve the result as alll matching only one object ?

you are using it as an object...

   $establishment_id->establishment_id,
 $city->city,
 $district->district

I don't understand what is your goal.

My goal is to retrieve one element using the primary key, but my primary key is made of three colums establishment_id, district and city.

I don't understand what is your goal.

Then your solution from first post is correct. What problem do you have ? Just check query being made by code i posted. Check if it's correct, if it is then just no record is resulted because there is no existing record for provided conditions.

My first approach was the good one, the problem was that I've treated parametters as objects , rathan than variables. Thank you guyz for helping. Case solved . I give the working code:

public function findAction($establishment_id, $city, $district) { echo "here";

    $branch= Branch::findFirst(
        [

            'conditions' => 'establishment_id= ?1 AND city=?2 AND district= ?3',
            'bind' =>[
                1=>$establishment_id,
                2=>$city,
                3=>$district
            ]
        ]
    );

    if ($branch === false) {
        $this->response->setJsonContent(
            [
                "status" => "NOT-FOUND"
            ]
        );
    } else {
        $this->response->setJsonContent(
            [
                "status" => "FOUND",
                "data"   => [
                  $branch
                ]
            ]
        );
    }

    return $this->response;
}

Then you know that you have this in logs that you accessing property of non object?