New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Composite primary key #6

Closed
olemara opened this Issue Jan 24, 2013 · 3 comments

Comments

Projects
None yet
3 participants
@olemara

olemara commented Jan 24, 2013

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

This comment has been minimized.

Show comment
Hide comment
@vitalets

vitalets Jan 28, 2013

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!

Owner

vitalets commented Jan 28, 2013

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

This comment has been minimized.

Show comment
Hide comment
@olemara

olemara Jan 28, 2013

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!

olemara commented Jan 28, 2013

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

This comment has been minimized.

Show comment
Hide comment
@tugas

tugas Mar 10, 2013

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

tugas commented Mar 10, 2013

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 May 18, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment