add column to select object zend framework - zend-framework

Here is my current code:
$Select=new Select();
$Select->from($this->getTable());
What I want now is to add the id column but as DT_RowId instead of id. How do I accomplish this? The goal would be to have all of the table columns as well as this new column.

If you need both "old" and "new" fields, don't forget to add an asterisk.
$Select=new \Zend\Db\Sql\Select();
$Select->from($this->getTable());
$Select->columns([
'*',
'DT_RowId' => 'id',
'furtherColumn' => 'furtherColumn'
]);

The simplest soloution would be to use the columns function with an associative array with aliases as the keys for example:
$select=new Select();
$select->from($this->getTable());
$select->columns(array(
'DT_RowId' => 'id',
'furtherColumn' => 'furtherColumn',
));

Related

Compound fields in search

I'm trying to get first name and last name from a related table
I've tried using an accessor on the related table which works fine when loading the page and for adding/editing but when searching it shows an error
Column not found: 1054 Unknown column 'FullName' in 'where clause'
This column obviously does not exist.
So I have this in my related model
{
return $this->attributes['first_name'] . ' ' . $this->attributes['last_name'];
}
And under the Backpack crud Controller I have
$this->crud->addColumn([ // n-n relationship (with pivot table)
'label' => 'Account', // Table column heading
'type' => 'select',
'name' => 'user_id', // the method that defines the relationship in your Model
'entity' => 'user', // the method that defines the relationship in your Model
'attribute' => 'FullName', // foreign key attribute that is shown to user
'model' => "App\Models\BackpackUser", // foreign key model
]);
Where am I making a mistake, input would be very much appreciated.
That needs a real column.
You can use a view (like you already do) or a model_function column to format the table cell value in your model class - https://backpackforlaravel.com/docs/3.5/crud-columns#model_function
Just see what's simpler for you.
Note: if ordering or searching doesn't work as expected see https://backpackforlaravel.com/docs/3.5/crud-columns#custom-search-logic-for-columns and https://backpackforlaravel.com/docs/3.5/crud-columns#custom-order-logic-for-columns

EF Fluent API - HasIndex for complex field

I need to use Fluent API (no DataAnnotation) and should create complex Index (3 fields). I try to do it:
this.HasIndex(p => p.LoadId).HasName("IX_UniqueLoad").IsUnique();
this.HasIndex(p => p.LocationId).HasName("IX_UniqueLoad").IsUnique();
this.HasIndex(p => p.StopAction).HasName("IX_UniqueLoad").IsUnique();
but it says:
The index with name 'IX_UniqueLoad' on table 'dbo.Stop' has the same
column order of '-1' specified for columns 'LoadId' and 'LocationId'.
Make sure a different order value is used for the IndexAttribute on
each column of a multi-column index.
How to do it?
I found solution:
this.HasIndex(p => new { p.LoadId, p.LocationId, p.StopAction }).IsUnique();
it will generates:
CreateIndex("dbo.Stop", new[] { "LoadId", "LocationId", "StopAction" }, unique: true);

How to unset field from Document

I have one document in which I want to remove one field. I got answer from Here. But, It is for core PHP code and I want in CakePHP how to unset field from document.
I tried following code in CakePHP.
$this->MyModal->id = $id;
$this->MyModal->updateAll(
array('myField' => array('$exists' => true)),
array('unset' => array('myField' => true))
);
Edit
I also tried
$this->MyModal->updateAll(
array('$unset' => array('myField' => 1))
);
But it does not work.How can unset field?
It looks like you're using wrong the update all method. First you must provide an array with the new values you want for your fields and then you should pass an array with the search criteria. So in order for it to work you could write something like
$conditions=array('myField'=>array('$exists'=>true));
$this->MyModal->updateAll(
array('$unset'=>array('myfield'=>1)),
$conditions//array of search conditions
)

How to perform add action in a form only one time, after update the record everytime

I have a form element. When I click the submit button it will add the data to the table.
that is working fine. But my need is to add data to the table only the first time, after each submission data should updated.how we can do this?
function add(){
if(!empty($this->data)){
$this->loadModel('Defineroute');
$this->Defineroute->create();
$this->Defineroute->save($route_data); //route_data is data from the form
}
}
I have one more doubts, I have a view.ctp in the controller I have a view() function for that. But in the view.ctp I use one form element.
How we can write function for that element in the controller? When I try to write function, it shows the error missing view..
CakePhp determines if it should update an existing record or insert a new record based on the presence of the primary key ('id') inside the data you're saving;
This will insert a new record:
$route_data = array(
'Defineroute' => array(
'name' => 'I am a new record'
)
);
$this->Defineroute->save($route_data);
And this will update an existing record;
$route_data = array(
'Defineroute' => array(
'id' => 123,
'name' => 'I will update record with ID 123'
)
);
$this->Defineroute->save($route_data);
To accommodate this in a single form, create the form inside your view/element and only add an input for the id field if you're editing an existing record
Also see Saving your data

Typo3 foreign_table & foreign_table_where in TCA

I am struggling with the following problem.
I have two database tables, "Books" and "Category". I am getting all the data from "books"-table via Sysfolder in Backends List-view for editing, sorting and controlling them.
What I would like to get, is that there would be in that list view also the name of the category where the book belongs.
In "Books"-table there is a field foreign-key "category_id" which defines that for which category the Book belongs. I have tried via this "category_id" to get the name of the Category in List-view of the Books.
When I define in TCA['books'] that category_id like:
'category_id' => array (
'exclude' => 0,
'label' => 'Cat name',
'config' => array (
'type' => 'select',
'foreign_table' => 'category',
'foreign_table_where' => 'AND category.id=###REC_FIELD_category_id###',
'eval' => 'int',
'checkbox' => '0',
'default' => 0
)
),
it connects the Books and Categories using category_id (in Books-table) and uid (in Category-table).
Not like I would like, that it would connect them using category_id(in Books-table) and id(in Category-table). This id is a id of the category and can be different that uid.
Am I doing something wrong or does Typo3 somehow automatically makes this "connection" to foreign tables uid. ? Is there some way to get it like I would like?
I'm afraid it's not possible to specify different foreign key. So unless somebody proves me wrong, here is a workaround that I would use.
itemsProcFunc of the select type allows you to completely override the items in the menu and thus create a different relation.
Create an extra class that will be loaded in the backend only and that will have the method that will be called in the itemsProcFunc:
yourMethod($PA, $fobj)
Make the method to load all the categories you want to have in the SELECT box and set them in the $PA['items'] by completely overriding it so that it is an array of arrays where the 0 key is element title and 1 key is the category ID that you want. See items in select.
$PA['items'] = array(array($title, $id, ''));
Include the class in the ext_tables.php:
if(TYPO3_MODE == 'BE') require_once(t3lib_extMgm::extPath($_EXTKEY).'class.YOUR_CLASS.php');
Set the config for the category field in the books table:
'itemsProcFunc' => 'tx_YOUR_CLASS->yourMethod',
In addition to the great answer of cascaval:
#cascaval: Do you mind pointing to the select.items in the Typo3TCA in the select links? The current links aren't straight forward.
http://docs.typo3.org/typo3cms/TCAReference/singlehtml/#columns-select-properties-items
(no permission to comment to your answer, so had to answer myself just for this link ... weird)