Adding fields on the fly - atk4

I have a form with a table source, the form has a few fields (product, quantity)
I'm thinking to add a button that allows me to add another "line" with product, quantity.... and so on, because I don't know how many items I need to add.
Which is the best approach ?
maybe removing the StaticSource('') and implementing on the submit the inserts ?
Thanks
Alejandro

class page_yourpage extends Page {
function init(){
parent::init();
$this->add("CRUD")->setModel("Product");
}
}
class Model_Product extends Model_Table {
$entity_code = "yourtable";
function init(){
parent::init();
$this->addField("product_id")->refModel("Model_Product");
$this->addField("quantity");
}
}
that's it. (did not test in browser);

Related

Cakephp 3 - reusable code for Table Entities

I have some code, that I need to apply for multiple Tables' Entities
similar to the example here
http://book.cakephp.org/3.0/en/orm/entities.html#accessors-mutators
protected function _setTitle($title)
{
// code to make re-usable
return $title;
}
Where can I move my code, so I can access it from multiple Entities. I tried a function inside Behavior, but it did not work.
Thanks
You can do this one of two ways. First, using a trait (a bit like what you were trying to achieve with a behavior):-
class Example extends Entity
{
use TitleTrait;
}
trait TitleTrait
{
protected function _setTitle($title)
{
return $title;
}
}
Second way is by using inheritance:-
class Example extends CustomEntity
{
}
abstract class CustomEntity extends Entity
{
protected function _setTitle($title)
{
return $title;
}
}

joining two models and show theme in one grid

I have two models:
A) Model ticket:
class Model_Ticket extends Model_Table {
public $table='ticket';
function init(){
parent::init();
$this->addField('subject');
$this->addField('content')->type("text");
}
}
B) Subticket:
class Model_Subticket extends Model_Table {
public $table='subticket';
function init(){
parent::init();
$this->addField('content')->type("text");
$this->addField('ticket_id')->type("int");
}
}
each ticket has many subtickets.
now I want to have a grid which the first row of that should be the "Content" of main ticket and other rows should be the "Content" of subtickets of that ticktet.
how can we do that?
thanks.
First create the grid which would display sub tickets, load them properly etc. Next look inside Grid's render() method. As you follow the execution, you'll find the following call chain:
grid::render()
CompleteLister::renderRows()
CompleteLister::renderDataRows()
You would need to override the normal rendering, fill out $this->current_row yourself and then call renderDataRows once before passing execution to parent::renderRows();
This will pop an extra row inside your Grid.

Laravel one-to-many relationship returns null

So here's what i have set up. I have two tables; users and todos. Every user can have multiple "todos".
Here's what the tables look like:
Users:
Todos:
And the models:
class User extends Eloquent
{
public function todo() {
$this->has_many('todo');
}
}
And...
class Todo extends Eloquent
{
public function user() {
$this->belongs_to('user');
}
}
Note that i already have a "todo" attached to the user in the database. So, with that said, should i not be able to do the following:
$user = User::find(1)->todo;
To get the todo's for that user? I'm currently getting Null when dd()-ing it:
array(1) {
["todo"]=>
NULL
}
So, any ideas? I tried removing belongs_to() from the Todo's model, because it shouldn't be needed right? Either way it makes no difference. Am i missing something?
You need to return the relation objects, e.g.
class User extends Eloquent
{
public function todos() {
return $this->has_many('Todo');
}
}
Also note that relations that return an array (e.g. has_many, has_many_and_belongs_to) are typically named plural, so 'todos' versus 'todo.

How to save data POSTed from From into database not using Form->setModel()

I know that's kinda simple and lame question, but still.
I have a Form which should not show all Model fields, but only some of them. That's why I can't use Form->setModel($m), because that'll automatically add all fields into Form.
So I add Model into page, then add form into page and then use form->importFields like this:
$m = $p->add('Model_Example');
$f = $p->add('Form');
//$f->setModel($m); // can't use this because that'll import all model fields
$f->importFields($m,array('id','description'));
$f->addSubmit('Save');
What I don't understand in this situation is - how to save this data in database, because $f->update() in onSubmit event will not work. Basically nothing I tried will work because Form have no associated Model (with setModel).
How about this way?
$your_form->setModel($model,array('name','email','age'));
I have solution for mixed form. Add custom form fields in form init and manipulate with them by hooks ('afterLoad','beforeSave')
In this case you can use setModel() method
$form->setModel('Some_Model',array('title','description'));
class Form_AddTask extends Form {
function init(){
parent::init();
$this->r=$this->addField('autocomplete/basic','contact');
$this->r->setModel('ContactEntity_My');
}
function setModel($model,$actual_fields=undefined){
parent::setModel($model,$actual_fields);
$this->model->addHook('afterLoad',array($this,'setContactId'));
$this->model->addHook('beforeSave',array($this,'setContactEntityId'));
return $this->model;
}
// set saved value for editing
function setContactId() {
$this->r->set($this->model->get('contact_entity_id'));
}
function setContactEntityId() {
$this->model->set('contact_entity_id',$this->get('contact'));
}
}
There is a hook 'validate' as well in Form_Basic::submitted(), so you can add
$this->addHook('validate',array($this,'validateCustomData'));
and validate your data in Form::validateCustomData()
Why not set the fields to hidden in the model?
I.e.:
class Model_Example extends Model_Table {
public $table='assessment';
function init() {
parent::init();
$grant->addField('hidden_field')->hidden(true);
}
}
And then:
$m = $p->add('Model_Example');
$f = $p->add('Form');
$f->setModel($m);

Why do I get Unable to include RenderObjectSuccess.php error when using inline in grid?

I'm setting up a grid with an inline field, pretty much the sames than in the examples in the demo, but I just keep getting error: Unable to include RenderObjectSuccess.php when chaning the value in the inline field. What a I doing wrong?
class page_alumnos_pago extends Page {
function init(){
parent::init();
$this->api->stickyGET('id');
$mAlumno=$this->add('Model_Alumno')->loadData($_GET['id']);
$g = $this->add('Grid');
$g->addColumn('date','fechaVencimiento');
$g->addColumn('text','concepto');
$g->addColumn('money','monto');
$g->addColumn('inline','temp_pago');
$g->setSource('programaPago');
$g->dq->where('alumno_id',$_GET['id']);
}
}
This is my ProgramaPago model:
class Model_ProgramaPago extends Model_Table {
public $entity_code='programaPago';
function init(){
parent::init();
$this->addField('alumno_id')->caption('Alumno')->refModel('Model_Alumno')->mandatory(true);
$this->addField('fechaVencimiento')->caption('Fecha de Vencimiento')->type('date')->mandatory(true);
$this->addField('monto')->caption('Monto')->type('money')->mandatory(true);
$this->addField('concepto')->caption('concepto')->type('text')->mandatory(true);
}
}
Eh, that's a bug.
https://github.com/atk4/atk4/issues/37
fix: https://github.com/atk4/atk4/commit/3694996
Please let me know if the fix works.

Resources