Compound fields in search - laravel-backpack

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

Related

Undefined array key "relation_type"

Im having some issues with a backpack project im doing.
What im trying to do change the way the PermissionsManager displays the check boxes when editing a user.
I have managed to create the custom CRUD files and i know that these files are working as i can edit some of the basic fields to add additional user fields.
The code im using to add the relationship is..
$this->crud->addField(
[ // relationship
'name' => 'permission', // the method on your model that defines the relationship
'type' => "relationship",
// OPTIONALS:
// 'label' => "Category",
// 'attribute' => "title", // attribute on model that is shown to user
// 'placeholder' => "Select a category", // placeholder for the select2 input
]
);
The error i get then i try to access the page is..
Undefined array key "relation_type"
Any ideas on what might be causing this?
thanks!
I think you are getting that error because the relationship name is permissions (plural) and not permission (singular).
Let me know if that's the case.
Cheers

DbiX::Class / Creating tree of test data without persisting

I'm using DBIx::Class as or mapper for my Perl project. When it comes to generating test data I'm using DBIx::Class::ResultSet::new to create new entities in memory. In order to link entities with relationships I use set_from_related. This works absolutely flawless until I try to set the value(s) for a has_many relationship. Pseudo example:
# Table 'AUTHOR' has
# one-to-one (belongs_to) relationship named 'country' to table 'COUNTRY'
# one-to-many (has_many) relationship named 'books' to table 'BOOK'
my $s = Schema::getSchema();
my $author = $s->resultset('Author')->new({ name => 'Jon Doe', year_of_birth => 1982 });
my $country = $s->resultset('Country')->new({ name => 'Germany', iso_3166_code => 'DE' });
my $book = $s->resultset('Book')->new({ title => 'A star far away', publishing_year => 2002 });
# Now let's make 'em known to each other
$author->set_from_related('country', $country);
$author->set_from_related('books', $book);
# At this point
# $author->country is defined
# $author->books->first is undef <<<---- Problem
I cannot find a suitable method in the DBIx::Class::Relationship::Base documentation. The one closest to what I need is add_to_$rel but this method creates (persists) the entities. This is not an option for me as some of the entites used in my project don't belong to me (no write permission).
Does anyone have an idea how to add entities in memory for a has_many relationship ?
That's not possible as newly created result objects don't have their primary key column(s) populated until they are persisted to the database.
What you possibly want is to use multi-create and run that inside a transaction.

Laravel Backpack: Show a Many to Many relationship in Show Operation

Couldn't find this in the docs.
Is there any standard way, without creating a custom widget, or overriding the view template, to show a Many to Many relationships in a CRUD's showOperation in Backpack for Laravel? If the answer is NO, what would be your approach to implement it?
Let's say I have a Course Model, and a User model, and there is a Many to Many between both
class Course extends Model
{
public function students()
{
return $this->belongsToMany(User::class, 'course_students');
}
}
class User extends Model
{
public function courses()
{
return $this->belongsToMany(Course::class, 'course_students');
}
}
In the Show Operation for the Course. How do I show a Table with all students?
Indeed, you can use the relationship column for this
Excerpt:
Output the related entries, no matter the relationship:
1-n relationships - outputs the name of its one connected entity;
n-n relationships - enumerates the names of all its connected entities;
Its name and definition is the same as for the relationship field
type:
[
// any type of relationship
'name' => 'tags', // name of relationship method in the model
'type' => 'relationship',
'label' => 'Tags', // Table column heading
// OPTIONAL
// 'entity' => 'tags', // the method that defines the relationship in your Model
// 'attribute' => 'name', // foreign key attribute that is shown to user
// 'model' => App\Models\Category::class, // foreign key model
],
Backpack tries to guess which attribute to show for the related item.
Something that the end-user will recognize as unique. If it's
something common like "name" or "title" it will guess it. If not, you
can manually specify the attribute inside the column definition, or
you can add public $identifiableAttribute = 'column_name'; to your
model, and Backpack will use that column as the one the user finds
identifiable. It will use it here, and it will use it everywhere you
haven't explicitly asked for a different attribute.

add column to select object 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',
));

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)