Error when using CRUD


#1

Hi all,
I am trying to replay some examples from the Agile documentation to learn. One of them is this:

<?php
require 'vendor/autoload.php';

// Define your data structure
class Offer extends \atk4\data\Model {

    public $table = 'offer';

    function init() {
        parent::init();

        // Persistence may not have structure, so we define here
        $this->addField('domain_name');
        $this->addFields(['contact_email', 'contact_phone']);
        $this->addField('date', ['type'=>'date']);
        $this->addField('offer', ['type'=>'money']);
        $this->addField('is_accepted', ['type'=>'boolean']);
    }
}

// Create Application object and initialize Admin Layout
$app = new \atk4\ui\App('Offer tracking system');
$app->initLayout('Centered');

// Connect to database and place a fully-interractive CRUD
$a = [];
$db = new \atk4\data\Persistence_Array($a);
$m = new Offer($db);
$form = $app->layout->add('CRUD');
$form->setModel($m);                 

That produces the error message:

Fatal error: Call to undefined method atk4\data\Persistence_Array::action() in /home/jlinkels/public_html/quickstart/vendor/atk4/data/src/Model.php on line 1567

However, when I use a Form instead of a CRUD everything works well. That is, replacing the last two lines in the above code:

$form = $app->layout->add('Form');
$form->setModel($m);    

Now as far I can see from examples, I did use the right way to set a model in a CRUD. The model seems to be fine as it works in the Form.

Is this a bug or my bad understanding of ATK?

jlinkels


#2

That’s because CRUD adds pagination and tries to use action() to calculate number of results, which Array persistence does not support.

A quick fix is to disable paginator:

add(['CRUD', 'paginator'=>false]);

It’s a bug though, should be fixed by someone :slight_smile:


#3

Ah. Thanks.
On the bright side… one learns a lot from searching for something like this. :grin:

And I should add (for other readers) that creating a Persistence_Array like I did does not actually enable you to add records. In order to add records use “SESSION” to store the data.

session_start();
$db = new \atk4\data\Persistence_Array($_SESSION);

jlinkels


#4

well, i suggest following code to save later on

session_start();
if(!isset($_SESSION['my_virtual_table']) $_SESSION['my_virtual_table']=[];
$db = new \atk4\data\Persistence_Array($_SESSION['my_virtual_table']);

It will save you later on when ATK will introduce memorize method in application…
Its a safer side to welcome new code for your existing project …


#5

Actually you could do this:


#6

@gowravvishwakarma: Yes, I know. It is better practice to create a separate array. I assume if you have a second array which uses the same indices the model is messed up badly. However this is merely a proof of concept.

@romaninsh: When trying to find the error I have tried every syntax variation. The one-liner you post here, and creating each object separately and then assigning the properties like I posted in my question. And everything in between. I was impressed with the easy of use and flexibility of ATK, really. Like I said before, I learned a lot,


#7

I’m glad you like it. If you have any other problems post again.