I need help in ATK4 CRUD. I have built a backend for a project using Agile Toolkit 4.1.3.
I have the following Model:
class Model_Product extends Model_Table
{
public $entity_code = 'product';
function init()
{
parent::init();
$this->addField('category_id')->refModel('Model_Category')->mandatory(true);
$this->addField('name')->mandatory(true);
$this->addField('picture_id')->refModel('Model_Picture')->mandatory(true);
$this->addField('short_description')->mandatory(true);
$this->addField('description')->type('text')->mandatory(true);
$this->addField('uploaded_at')->type('datetime');
$this->addField('price')->type('int')->mandatory(true);
$this->addField('quantity')->type('int')->mandatory(true);
$this->addField('status')->datatype('list')
->listData(array(
'enabled'=>'Enabled',
'disabled'=>'Disabled',
))
->defaultValue('enabled');
}
}
the page:
<?php
class page_index extends Page {
function init(){
parent::init();
$page=$this;
$tabs = $page->add('Tabs');
$tabs->addTab('Product')->add('CRUD')->setModel('Product');
....
On my localhost all CRUD functions work flawlessly, but after I uploaded the files to the webserver when I try to add a new product I get this error:
`Error in AJAX response: SyntaxError: invalid XML attribute value
SQLException
Could not execute query: insert into product (category_id
, name
, picture_id
, short_description
, description
, uploaded_at
, price
, quantity
, status
) values (NULL, 'as', NULL, '', '', NULL, 2500, 25, 'enabled')
Last query:
insert into product (category_id
, name
, picture_id
, short_description
, description
, uploaded_at
, price
, quantity
, status
) values (NULL, 'as', NULL, '', '', NULL, 2500, 25, 'enabled')
MySQL error:
Column 'category_id' cannot be null`
Strange thing that the missing values in the query are visible in the crud form but never make it to the query. Additional info: in Model_Picture I use varchar id field instead of autoincrement int but once again everything works fine on localhost.
Thanks!
I had all my crud functions on one page on tabs like this:
<?php
class page_index extends Page {
function init(){
parent::init();
$page=$this;
$tabs = $page->add('Tabs');
$tabs->addTab('Product')->add('CRUD')->setModel('Product');
$tabs->addTab('Category')->add('CRUD')->setModel('Category');
$tabs->addTab('Property')->add('CRUD')->setModel('Property');
$tabs->addTab('Property <> Product')->add('CRUD')->setModel('ProductProperty');
$tabs->addTab('Payment options')->add('CRUD')->setModel('Payment');
$tabs->addTab('Shipping options')->add('CRUD')->setModel('Shipping');
$tabs->addTab('Users')->add('CRUD')->setModel('User');
$tabs->addTab('Email addresses')->add('CRUD')->setModel('Email');
$tabs->addTab('News')->add('CRUD')->setModel('News');
$tabs->addTab('Downloads')->add('CRUD')->setModel('Download');
$tabs->addTab('Designer Aids')->add('CRUD')->setModel('Aid');
}
}
Then I created a new page for every Model and added only one CRUD on each page and it solved the problem, now it works fine both on localhost and the server.
Thank you guys for all your help.
Which platform are you developing on and which platform are you deploying to online. You can see my previous question which had a similar error when developing locally on windows and then moving to a linux host online here - ATK4 model not found when moving to online
The error in that case was related to case sensitive issues in Linux whereas Windows is more forgiving of case differences so check your initial capitilsaton in your code.
I also recently got some errors from Ajax with CRUD in 4.1.3 when i included a closing ?>
in the class files. agiletoolkit.org recommends you dont use the closing tags as it can introduce invalid characters in the output which breaks some of the functionality so just ensure you are using <?php
at the top of each file but omit any ?>
on the last line.
i am also puzzled as to how a mandatory field:
$this->addField('description')->type('text')->mandatory(true);
end up being passed as a NULL value in your SQL insert string:
(NULL, 'as', NULL, '', '', NULL, 2500, 25, 'enabled')
ATK4 should already give a notification that this field is required and would never lead to an update()
attempt if the required fields are still empty.
do you have by any chance, a beforeInsert()
or beforeModify()
method defined that somehow manipulates this data prior to inserting?
as for the NULL
value for category_id
, when adding a new entry, by default, no items are selected for both:
$this->addField('category_id')->refModel('Model_Category')->mandatory(true);
and
$this->addField('picture_id')->refModel('Model_Picture')->mandatory(true);
so when adding a new entry, the user has to explicitly select from the list first.
for me, if all else fails, i rely on the beforeModify() method and throw a js()->fieldError notification:
try inserting this in your model:
function beforeModify(&$data){
if(empty($data['category_id'])){
$this->owner->owner->js()->atk4_form('fieldError','category_id',
'Select an category from the list')->execute();
exit;
}
}
please note that you may have to experiment on the $this->owner->owner
to arrive at the MVCForm object.
also, the exit()
call is important to prevent the internal render()
method from producing the HTML page and only produce the Javascript Chain.
I hope this helps.