Skip to content
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

Fatal Error in Custom Finder Method #3950

Closed
ghost opened this issue Jul 11, 2014 · 4 comments
Closed

Fatal Error in Custom Finder Method #3950

ghost opened this issue Jul 11, 2014 · 4 comments
Milestone

Comments

@ghost
Copy link

ghost commented Jul 11, 2014

Warning (2): array_fill(): Number of elements must be positive [ROOT\vendor\cakephp\cakephp\src\ORM\Table.php, line 1238]
Code Context
    protected function _insert($entity, $data) {

        $primary = (array)$this->primaryKey();

        $keys = array_fill(0, count($primary), null);
$entity = object(App\Model\Entity\VolunteersVolunteerType) {

    'new' => true,
    'accessible' => [
        'volunteer_id' => true,
        'volunteer_type_id' => true,
        'volunteer' => true,
        'volunteer_type' => true
    ],
    'properties' => [
        'volunteer_id' => (int) 19,
        'volunteer_type_id' => (int) 1
    ],
    'dirty' => [
        'volunteer_id' => true,
        'volunteer_type_id' => true
    ],
    'virtual' => [],
    'errors' => [],
    'repository' => 'VolunteersVolunteerTypes'

}
$data = [
    'volunteer_id' => (int) 19,
    'volunteer_type_id' => (int) 1
]
$primary = []
array_fill - [internal], line ??
Cake\ORM\Table::_insert() - ROOT\vendor\cakephp\cakephp\src\ORM\Table.php, line 1238
Cake\ORM\Table::_processSave() - ROOT\vendor\cakephp\cakephp\src\ORM\Table.php, line 1195
Cake\ORM\Table::Cake\ORM\{closure}() - ROOT\vendor\cakephp\cakephp\src\ORM\Table.php, line 1131
Cake\Database\Connection::transactional() - ROOT\vendor\cakephp\cakephp\src\Database\Connection.php, line 495
Cake\ORM\Table::save() - ROOT\vendor\cakephp\cakephp\src\ORM\Table.php, line 1132
App\Model\Table\VolunteersVolunteerTypesTable::addType() - APP/Model\Table\VolunteersVolunteerTypesTable.php, line 54
App\Controller\VolunteersController::registerIndividual() - APP/Controller\VolunteersController.php, line 233
ReflectionMethod::invokeArgs() - [internal], line ??
Cake\Controller\Controller::invokeAction() - ROOT\vendor\cakephp\cakephp\src\Controller\Controller.php, line 374
Cake\Routing\Dispatcher::_invoke() - ROOT\vendor\cakephp\cakephp\src\Routing\Dispatcher.php, line 115
Cake\Routing\Dispatcher::dispatch() - ROOT\vendor\cakephp\cakephp\src\Routing\Dispatcher.php, line 87
[main] - ROOT\webroot\index.php, line 38

Custom Finder Method

public function addType($id = null, $type = null){
        if (!$id || !$type) {
            return false;
        }else{           
            $record = $this->newEntity();
            $record->volunteer_id = (int)$id;
            $record->volunteer_type_id = (int)$type;
            if ($this->save($record)) {
                return true;
            } else {
                return false;
            }
        }
    }
@ghost
Copy link
Author

ghost commented Jul 11, 2014

I have no primary key defined in VolunteersVolunteerTypesTable because it is a join table:

public function initialize(array $config) {
        $this->table('volunteers_volunteer_types');

        $this->belongsTo('Volunteers', [
            'foreignKey' => 'volunteer_id',
        ]);
        $this->belongsTo('VolunteerTypes', [
            'foreignKey' => 'volunteer_type_id',
        ]);
    }

vendor\cakephp\cakephp\src\ORM\Table.php Line: 1239

$id = (array)$this->_newId($primary) + $keys;

@ghost
Copy link
Author

ghost commented Jul 11, 2014

Adding a primary key to the table made the error go away. But as a hasMany join table that id won't be unique. How can I solve this?

@markstory markstory added this to the 3.0.0 milestone Jul 12, 2014
@markstory
Copy link
Member

All tables need a primary key. You can use composite primary keys in 3.0.

The error isn't very helpful, and it would be better if the ORM raised an exception when a table had no primary key and you attempted to save a record.

markstory added a commit to markstory/cakephp that referenced this issue Jul 12, 2014
If we don't have a primary key we cannot safely insert/update records.

Refs cakephp#3950
@markstory
Copy link
Member

Closing as there is an open pull request now.

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

No branches or pull requests

1 participant