Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Composite primary key #6

Closed
olemara opened this Issue · 3 comments

3 participants

@olemara

Your component looks very useful.

I ran across the problem of having a composite primary key:

public function getSelector()
{
return get_class($this->model) . '' . $this->attribute . ($this->model->primaryKey ? '' . $this->model->primaryKey : '_new');
}

Any smooth workaround for this?

@vitalets
Owner

hi, it's definitely a bug,
Composite key should be converted to string here. something like:

if(is_array($this->model->primaryKey)) {
  foreach($this->model->primaryKey as $k => $v) {
     $selector .= $k.'_'.$v;
  }
}

If you test that it works correct I will appreciate pr.
Thanks!

@olemara

Thanks for the reply,

Yeah somthing like that. My temporary solution was to have a helper function:

    private function getPrimaryKeyAsString() {
        $pkString = NULL;
        if (is_array($this->model->primaryKey)) {
            $isFirst = TRUE;
            foreach ($this->model->primaryKey as $k => $v) {
                if ($isFirst) {
                    $pkString .= $k.'_'.$v;
                    $isFirst = FALSE;
                }
                else {
                    $pkString .= '-'.$k.'_'.$v;
                }
            }
        }
        else {
            $pkString .= $this->attribute . ($this->model->primaryKey ? '_' . $this->model->primaryKey : '_new');
        }
        
        return $pkString;
    }

This is because it needs to be used in buildHtmlOptions() when setting $htmlOptions['data-pk'] if composite.

        $htmlOptions = array(
            'href'      => '#',
            'rel'       => $this->getSelector(),
//            'data-pk'   => $this->model->primaryKey,
        );
        
        if (is_array($this->model->primaryKey)) {
            $htmlOptions['data-pk'] = $this->getPrimaryKeyAsString();
        }
        else {
            $htmlOptions['data-pk'] = $this->model->primaryKey;
        }

htmlspecialchars does not like arrays.

Cheers!

@tugas

I have commend across this problem what i did was the following

In the file EditableField.php

public function getSelector()
{
if (is_array($this->model->primaryKey)){
return get_class($this->model) . '' . $this->attribute . ($this->model->primaryKey ? '' . CJSON::encode($this->model->primaryKey) : 'new');
}else{
return get_class($this->model) . '
' . $this->attribute . ($this->model->primaryKey ? '_' . $this->model->primaryKey : '_new');
}

}

Line 330
public function buildHtmlOptions()
{
//html options
$htmlOptions = array(
'href' => '#',
'rel' => $this->getSelector(),
'data-pk' => is_array($this->model->primaryKey) ? CJSON::encode($this->model->primaryKey) :$this->model->primaryKey ,
);
.....

The request will be sent to the server like an array pk={id1:value,id2:value }
Sorry for my bad writing skills in github comments
I hope this helps
Regards

@vitalets vitalets closed this in 8cda49d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.