additionals properties of a model in cakephp? - php

I'm starting to use cakePhp and I would like to know:
When I do a query(like with $this->Product->find(...)), I receive an array, right?
Then:
-If I've some non-db fields with default values that I've to display for each product, how do I do?
I saw virtual fields, but I cannot manage to put a static var into it

You can use the solution Anh Pham provided another thing you could do, if it's just setting the values to display them, is add them to the array in your controller after you have the results:
$result['Model']['desired_field_name'] = $static_field;

Related

Laravel: How to dynamically create and name views?

I am making a website for college administration where professors log in and assign marks to the students they are teaching.
There's a table, called "IA_Marks" in my database:
|Student_ID|Subject_Code|Name|Marks1|Marks2|Marks3|Semester|Division|
There's also a table called "Classroom_Mapper" in my database, that helps map a professor to a classroom, with a subject:
|Prof_ID|Subject_Code|Semester|Division|
This is a method in my controller:
public function showTable(){
$sem = DB::table('classroom_mappers')->where('Prof_ID', auth()->user()->PID)->pluck('semester');
$division = DB::table('classroom_mappers')->where('Prof_ID', auth()->user()->PID)->pluck('division');
$data = DB::table('iamarks')->where([['semester','=',$sem],['division','=',$division]])->get();
return view('ia',compact('data'));
}
Using this, I can fetch rows that belong to the professor who has logged in.
But, there's a problem.
Say the professor teaches two subjects, in two semesters. Then, the where clause will return multiple results from the mapper table.
For example:
select semester from classroom_mapper where Prof_ID=auth()->user()->Prof_ID
output:
8
5
Then the students from both 5th and 8th semester will be shown on his dashboard. Our target semester was, say 5th. Then it'll be a problem.
Registering for a subject, is done as shown here:
form screenshot
Let's call the subject being registered in the screenshot "SUBJECT 4".
It is a subject for the 5th semester, division A.
I want to dynamically make a button(SUBJECT 4) on the dashboard, which when clicked, sends the semester(5) and division(A) of choice to the controller.
Dashboard Screenshot
This button should open a newly made page with name of the subject(subject4.blade.php), where the database table contents for target semester and division(5 and A) will be shown.
How do I make this dynamic view creating button which sends specific info to controller? Is it even possible?
There are a few ways to do this with Laravel, but my goto is usually to create a single blade template for each view (dashboard, subject, etc.) that can be dynamically populated -- assuming that the layout for each subject view is the same.
In your dashboard view, you could generate a url for each button that uses a format like this: http://cas.jce.in/subject/semester/5/division/a/
Next, create a route that uses a couple of paramaters, something like this:
Route::get('/subject/semester/{semester_id}/division/{division_id}', 'ControllerName#showSubject');
More info here: https://laravel.com/docs/5.8/routing#required-parameters
Then in your controller, add a showSemester function like this:
function showSubject($semester_id, $division_id){
$data = DB::table('table_name')->where('semester', '=', $semester_id)->where('division', '=', $division_id)->first();
return view('subject', ['data'=>$data, 'semester'=>$semester_id, 'division'=>$division_id]);
}
Your route parameters are available to the controller, in order of appearance. So we can add $semester_id and $division_id as the first two parameters of our function. Next, we'll to the database work to retrieve the data we need before returning everything to a view.
Note here that we're using a single view rather than dynamically selecting one. You could create individual views for each subject, but im thinking you probably don't need to unless the layout of each one is unique in some way. In that case, you can simply do something like this, but I'd generally try to avoid it.
$view = 'subject'.$data->subject_id;
return view($view, ['data'=>$data, 'semester'=>$semester_id, 'division'=>$division_id]);
Also, just a quick note ... you may consider adjusting your database queries from above to use a select statement rather than pluck. The end result is the same, but using a select can boost performance by only loading the data you want ... rather than loading everything up front and throwing most of it away.
$sem = DB::table('classroom_mappers')->where('Prof_ID', Auth()->user()->PID)->pluck('semester');
... becomes ...
$sem = DB::table('classroom_mappers')->select('semester')->where('Prof_ID', auth()->user()->PID)->get();

pimcore related object offset

I am working on a pimcore project (version 4.4.3) but still pretty new to pimcore itself.
First I made an Object called 'Event' in the admin panel and added a data component -> relation -> Object called 'speakers'.
Now I have a controllerAction which needs to return these speakers, but I don't want them all at once so I wish to add a limit and offset.
The result of $eventClass->getSpeakers() returns an array with objects on which I don't seem to be able to put any filters.
Of course I can filter them after I retrieved all of them, but if possible I would like to filter them in my request.
So my question is, how do I filter the related objects on my object?
I'm afraid that currently you can do it only using SQL. It will look something like this:
SELECT dest_id FROM object_relations_5 where fieldname = 'speakers' and src_id = 123 LIMIT 10;
Where 5 should be your class' id and 123 your object's id. You can join other tables to do more filtering, but it's getting complicated.
Commonly if you have to write custom SQL code, something is wrong with your data model. Maybe your "speaker" class should have a single href relation to "event" - this way you could get speakers listing easily with all filtering you want.
You can use the Listing object for that
$speakerId = 123;
$list = new \Pimcore\Model\Object\Event\Listing();
$list->setCondition("speakers like '%,".$speakerId.",%'");
But you can only filters the speakers with their IDs only. If you want to filter them with some other attributes then you have to make a join with object_relations_ClassID table.
Also have a look at the following link
https://pimcore.com/docs/4.6.x/Development_Documentation/Objects/Object_Classes/Data_Types/Relation_Types.html#page_Filtering-for-relations-via-PHP-api

Populate a Select Box with Eagerloading

Any idea what the best way would be to populate a select box with eagerloading?
For example: I can eagerload a hasOne relation like so which works fine for showing the initial status.
Order Model
public function status()
{
return $this->hasOne('OrderStatus','id','status');
}
But then on the order page I also need to populate a select box with all statuses so a user can change the order status.
Order Status Model
public function allStatuses()
{
return OrderStatuses::all();
}
and then I try calling with the dot notation, but that does not work
$order::with('orderStatus','orderStatus.allStatuses')->whereId(1000)->get();
Basically, my views contain a lot of drop-down for things like country, state, etc. I know I can send them through the view with multiple variables, but I would like to just call everything in one collection to the view if possible.
I know the above is probably wrong, but any suggestion on the best way to do this would be appreciated.
Right now I am calling all my common selects from a service provider and then import my repos into that so I can call directly from my view like FormList::getStates(), FormList::getCountries(), etc.. Not sure if this is the best way.

Laravel Query - pulling value from array

this has been driving me nuts...
I'm in my view I'm calling
Auth::user()->userlists
which brings me back an array of values like
{"id":"1","user_id":"1","name":"list1".....}
all I want to do is bring back the names I've tried all these;
Auth::user()->userlists->name
Auth::user()->userlists()->name
Auth::user()->userlists->name->get()
Auth::user()->userlists.name
But I always get an error such as "Undefined property"
How do I return this single property, it's in my array for all the items but I'm clearly just getting the syntax incorrect...?
The reason i'm trying to do this is that I need the values placed in a drop down box, it's finding the correct rows in the table but displaying all the data instead of just the name
Form::select('userlist_id', Auth::user()->userlists);
Many thanks.
You say a User has many Userlist models, so it's a collection not single model, thus you could work with it in a loop, BUT there is better way:
$lists = Auth::user()->userlists()->lists('name','id');
// To make it available in all the views, place this for example in a controller:
View::share('userlists', $lists);
This will fetch id and name for the related models collection and return it as an array, so it's just the one you will use in a Form builder:
Form::select('userlist_id', $lists)

Symfony sfWidgetFormDoctrineChoice displays id numbers instead of actual values

I have a user table and an article table. When creating a new Article using Symfony's generated forms, the User dropdown/select shows numbers (user id numbers like 1, 2, 3...) instead of User Names.
I can obviously overwrite the form with a custom one in order to make sure this select element is populated with user names instead of their ID numbers, but I figured Symfony would have maybe already figured this one out and there was just something I was missing...
Should it show names by default or did I do something wrong?
Actually, it will call the model's __toString() method. You can override it in your models to return any string value.
class User extends BaseUser {
public function __toString() {
return $this->getUsername();
}
}
Alternatively, you can pass the 'method' option to the choice widget. For example:
new sfWidgetFormDoctrineChoice(array('Model' => 'User', 'method' => 'getUsername'))
I figured out the problem myself. Basically if you have a table column called name, then Symfony will automatically use this field to populate a dropdown menu. I happen to be using username. Pretty cool design idea, but I don't remember reading it in the Symfony docs. Oh well.

Categories