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.

Bind Params

Hi all, i'm doing something stupid here but i can't realize it :P Please tell me why whis code works:

    public function loadEntity(){
        //Checking provided primary key ref syntax ok
        $id = $this->request->getQuery("id", "int", "");
          if($id == ""){
              throw new \Exception("Missing or malformed parameter for 'id'");
          }
        $Notification = $this->getUser()->getNotifications("id=$id")->getFirst();
        if(!$Notification){
            throw new \Exception("The requested object doesn't exists");
        }

        return $Notification;
    }

While this not:

        $search_array = array('id=?0',
                        'bind' => array($id),
                        );
        $Notification = $this->getUser()->getNotifications($search_array)->getFirst();

The first snippet returns the Related Id=$id child, the second reply "The requested object...".

Here the model function getNotifications:

        public function getNotifications($parameters=null){
            return $this->getRelated("Notifications", $parameters);
        }

Please help me understand :) Regards Gianluca

Can you try this?

    $search_array = [
         'conditions' => 'id=?0',
         'bind' => [$id],
    ];


11.0k
edited Feb '15

It's the same, tested with both

        $search_array = [
         'conditions' => 'id=?0',
         'bind' => [$id],
    ];

and

        $search_array = array(
                        'conditions' => 'id=?0',
                        'bind' => array($id),
                        );


1.4k
Accepted
answer
edited Feb '15

because it should be https://github.com/phalcon/cphalcon/blob/2.0.0/phalcon/mvc/model/manager.zep#L1289

basically the logic of binding it is a bit screwed.

you cant use ?0 since ?0 is already used by the system

if preConditions !== null {
    let conditions = [preConditions];
} else {
    let conditions = [];
}

if relation->isThrough() {

let intermediateModel = relation->getIntermediateModel(),
intermediateFields = relation->getIntermediateFields();

let fields = relation->getFields();
  if typeof fields != "array" {
  let conditions[] = "[" . intermediateModel . "].[" . intermediateFields . "] = :related:",  // << this needs to change
    placeholders['related'] = record->readAttribute(fields);
  } else {
    throw new Exception("Not supported");
}

meaning to say this is valid

  $search_array = array(
                        'conditions' => 'id= :uid:',
                        'bind' => array('uid' => $id),
                        );
edited Feb '15
  • edit

Don't mind me, I completely misread the OP. 7thcube is right.



11.0k
edited Feb '15

Hi 7thcubic,

first of all thanks for the investigation.

Last two questions?

1) Is safe to use the same name of the column for the identifier between the columns? (id= :id:)

2) Is this going to be fixed in Phalcon 2?

Thanks

I'm not 7th, I know but I I think I can answer the first question for you.

It shouldn't be a problem, I've done that alot in my own code. Phalcon basically recognises anything in the string that's like :value: as a parameter. It will complain if you forget the closing colon (:) which has happened to me more than once coming from Kohana where it was just :value.

I don't know about your second question though, I haven't tried 2.0 yet.



11.0k

Thanks for your reply, the main question has been answer! I'll have to replace all ?n with :names:!



1.4k

i will look into having a fix for 2.0.

basically it is safe to use anything except ?0 so ?1 onwards is good.