We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Records are not added

I'm moving a project from laravel to phalcon, code is almost the same, and one of the actions is parsing data from file and add content to database. File has 1883 records (relatively small for my project) but whatever I do there are only 18 records add to db. Here is the code of action that is doing all job:

    public function upgradeAction()
    {
        $this->view->disable();

        $lastLon = $lastLat = $lastPrefix = $lastCq = $lastItu = $lastContinent = $lastTz = $lastMask = '';

        if ($this->request->isPost()) {
            if ($this->request->isAjax()) {
                $filePath = 'uploads/pfx/'.$this->request->getPost('fileName');
                if (file_exists($filePath)) {
                    if (Prefixes::count()) {
                       Prefixes::find()->delete();
                    }
                    $prefixHelper = new PrefixHelper();
                    if ($content = (new PHPhelper())->splitFile($filePath)) {
                        foreach ($content as $row) {
                            $p = new Prefixes();
                            $p->InternalUse = $prefixHelper->removeLeading($row[0]);
                            $p->lon = $lastLon = ($row[1] != '') ? $prefixHelper->toCoords($row[1]) : $lastLon;
                            $p->lat = $lastLat = ($row[2] != '') ? $prefixHelper->toCoords($row[2]) : $lastLat;
                            $p->territory = ($row[3]) ? : '';
                            $p->prefix = $lastPrefix = ($row[4] != '') ? $prefixHelper->maskToPattern($row[4]) : $lastPrefix;
                            $p->cq = $lastCq = ($row[5] != '') ? $row[5] : $lastCq;
                            $p->itu = $lastItu = ($row[6] != '') ? $row[6] : $lastItu;
                            $p->continent = $lastContinent = ($row[7] != '') ? $row[7] : $lastContinent;
                            $p->tz = $lastTz = ($row[8] != '') ? $row[8] : $lastTz;
                            $p->adif = ($row[9]) ? : '';
                            $p->province = ($row[10]) ? : '';
                            $p->mask = $lastMask = ($row[13] != '') ? $prefixHelper->maskToPattern($row[13]) : $lastMask;
                            $success[] = $p->save();
                        }
                    }
                }
            }
        }
                                var_dump($success);
    }

I don't see much of a difference between records that are add from those left behind. Besides, everything is copied from the same controller in laravel where it works flowlessly. I can add more code upon request. Please help!

edited Apr '15

Check if saving the record is returning false and then check the messages:

if (!$p->save()) {
   print_r($p->getMessages());
}


5.2k

Thank you for answer. Here is one of responses:

    [0] => Array
        (
            [0] => Phalcon\Mvc\Model\Message Object
                (
                    [_type:protected] => PresenceOf
                    [_message:protected] => lon is required
                    [_field:protected] => lon
                    [_model:protected] => 
                    [_code:protected] => 0
                )

            [1] => Phalcon\Mvc\Model\Message Object
                (
                    [_type:protected] => PresenceOf
                    [_message:protected] => lat is required
                    [_field:protected] => lat
                    [_model:protected] => 
                    [_code:protected] => 0
                )

            [2] => Phalcon\Mvc\Model\Message Object
                (
                    [_type:protected] => PresenceOf
                    [_message:protected] => cq is required
                    [_field:protected] => cq
                    [_model:protected] => 
                    [_code:protected] => 0
                )

            [3] => Phalcon\Mvc\Model\Message Object
                (
                    [_type:protected] => PresenceOf
                    [_message:protected] => itu is required
                    [_field:protected] => itu
                    [_model:protected] => 
                    [_code:protected] => 0
                )

            [4] => Phalcon\Mvc\Model\Message Object
                (
                    [_type:protected] => PresenceOf
                    [_message:protected] => continent is required
                    [_field:protected] => continent
                    [_model:protected] => 
                    [_code:protected] => 0
                )

            [5] => Phalcon\Mvc\Model\Message Object
                (
                    [_type:protected] => PresenceOf
                    [_message:protected] => tz is required
                    [_field:protected] => tz
                    [_model:protected] => 
                    [_code:protected] => 0
                )

            [6] => Phalcon\Mvc\Model\Message Object
                (
                    [_type:protected] => PresenceOf
                    [_message:protected] => adif is required
                    [_field:protected] => adif
                    [_model:protected] => 
                    [_code:protected] => 0
                )

            [7] => Phalcon\Mvc\Model\Message Object
                (
                    [_type:protected] => PresenceOf
                    [_message:protected] => province is required
                    [_field:protected] => province
                    [_model:protected] => 
                    [_code:protected] => 0
                )

        )

But it brings more questions than answers, does that mean that I cannot add records with empty values? or should I change something in table definitions?



5.2k

YAHOO! it works now I have changed definitions on mysql table by allowing null values to fields and everything started to work. However, as I sayed this is one of the smallest chunks that I need to work with, the average chunk is 5000-10000 records but can be even more. And these backups should be preprocessed before upload. Is Phalcon providing any macanizm of uploading large chunks of records to the db? like chunking in laravel or some othe type?