joining two models and show theme in one grid - atk4

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.

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;
}
}

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);

Adding fields on the fly

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);

GWT: FlexTable swaping rows with Up/Down Buttons

i want to design a FlexTable programmaticaly which looks like this: http://uploadz.eu/images/v4z6gucp2gg8ql9pzj.png
Since OnClick Methods can't get other Parameters then an Event and the buttons don't know "on which row they are", i can't tell the button "please swap the row, where you currently are with your buddy on top of you".
I would like to know: What's a good way to do this?
ClickEvents have a getSource() method which contains the source of the event, which is helpful.
public class NiceLabel {
public final Row row;
public NiceLabel(Row row, ClickHandler clickHandler) {
this.row = row;
addClickHandler(clickHandler);
}
}
...
// clickHandler has the following:
public void onClick(ClickEvent event) {
((NiceLabel) event.getSource()).row.moveUp();
}
And in this way you can add the same ClickHandler to every NiceLabel without having to create a new one for each row.

Resources