I have an User which has an Avatar and an Armory. The User can only have one Avatar (at the moment) and the Armory can hold any number of Armor. This all works great. However, the Avatar needs to have a Head, Chest, Arms, etc so I created head_id, chest_id, etc on the avatar table. Basically those represent the current piece they are wearing in each position. The Armory is the pieces they can choose from.
So in my Avatar model I have:
public function head()
{
return $this->hasOne('Armor', 'id', 'head_id')
}
Which works just fine and returns me the piece of armor. The issue becomes when I POST data and try to change that piece of armor. What I do is:
$this->head_id = $newHeadID;
Which does actually change head_id, but if I then do $this->head(), it still returns the old head. I've tried $this->save() and $this->push() in-between, but it still doesn't update it immediately. The only way that seems to work is to entirely "re-query" like such:
$head = Armor::where('id', $this->head_id)->first();
I'm pretty sure I'm missing something like not doing an update, refresh, etc. Any ideas?
Related
I'm building a Laravel 9x application where i will need to populate some pages with the google places API.
Reading at the ToS of Google Places, i can only store in my db, for at most 30 days, the place_id.
Now, I'm facing an issue; I have a restaurants.index blade page where I'm showing in a table all the records from the restaurants table.
The issue is that i need to show in each table row the name of the restaurant that i don't have in my db table but i need to fetch it from Google Places Api.
So far, in my RestaurantsController I have done the following :
public function index()
{
$getAllRecords = Restaurant::all();
$google_places_api = new PlacesApi(env('GOOGLE_MAPS_API_KEY'));
$restaurants = new Collection();
foreach($getAllRecords as $restaurant){
$response= $google_places_api->placeDetails($restaurant->google_place_id);
$restaurant['name'] = $response['result']['name'];
$restaurants->prepend($restaurant);
}
dd($restaurants);//When dd here, the collection shows correctly the added value.
$restaurants->paginate(8);
$categories = Category::where('type', 'Restaurant')->cursor();
return view('tenant.restaurants.index', compact('categories', 'restaurants'));
}
And it seems to work pretty well if I dd($restaurants) inside the RestaurantsController as you can see from the picture below :
But when i try to #dd($restaurants) from the restaurants.index page, the added name is completely disappeared as you can see from the picture below :
It's now about a whole day that I'm tryng to understand why this behavior happen, is there anybody who has an idea of what is happening?
Thank you
SilverStripe: v4
Module used: https://github.com/unclecheese/silverstripe-display-logic
Hello and good day!
I'm in the Security page (Lists of Members) and was trying to display a ListboxField upon a certain string or value is found on another ListboxField.
.
With The image above, I wanted to select or input in the Groups field the Sales Representative role, and once the Sales Representative is present in the Groups, that's the moment I shall display another field
Here's my code snippet
$codesList = Member::get()->column('Code');
$codes = ListboxField::create(
'AccountPurchases',
'Account Purchases',
$codesList);
$fields->insertAfter('DirectGroups', $codes);
$codes->displayIf("DirectGroups")->contains("Sales Representative");
But the problem is, no matter what I did (1: dev/build?flush=1, 2: refresh the page, 3: restart the app) the desired field named $codes still won't appear.
But if I'm applying it to a normal field like the FirstName (which is a simple TextField), it's working perfectly fine...
$codesList = Member::get()->column('Code');
$codes = ListboxField::create(
'AccountPurchases',
'Account Purchases',
$codesList);
$fields->insertAfter('DirectGroups', $codes);
$codes->displayIf("FirstName")->contains("Sales Representative");
Any ideas how to perform my desired output?
For non-standard form fields, you'll need to use the display-logic wrapper. https://github.com/unclecheese/silverstripe-display-logic#dealing-with-non-standard-form-fields
The definition of "non-standard" is a bit hard to find, but in my experience it's anything non-plain-html-input. Listbox is a fancy front-end thing, so it qualifies. The examples in the docs can be confusing, but if you are using SS4 and the latest display-logic, then use Wrapper::create instead of DisplayLogicWrapper::create. If you wrap it, it should work.
$codesList = Member::get()->column('Code');
$codes = Wrapper::create(
ListboxField::create('AccountPurchases','Account Purchases',$codesList);
)->displayIf("DirectGroups")->contains("Sales Representative")->end();
$fields->insertAfter('DirectGroups', $codes);
I want to trigger an event when a category is added or deleted.
So I think the add_action i look for are these
create_category
delete_category
(got them from here source)
In the Event I want to have a function where the ID and the name of the category is sent to me.
At the moment I am struggling with the delete operation. The Id seems to delivered properly. The category "name" unfortunately not.
My guess is at the moment that the database no longer contains the name of the category. Probably because the delete_category is invoked after the job is already done. Therefore "get_cat_name()" might not work. Unfortunately I could not find some kind of "pre_delte_category" action.
Here are the functions i placed as hooks in my functions.php of my theme:
function event_by_add_cat($category_id){
$name = get_cat_name($category_id);
send_me_mail_add($name, $category_id);
}
add_action('create_category', 'event_by_add_cat');
function event_by_del_cat($category_id){
$name = get_cat_name($category_id); // is name already to far gone?
send_me_mail_del($name, category_id);
return;
}
add_action('delete_category', 'event_by_del_cat');
The function send_me_mail_add(name, id) does work.
It seems that the function is send_me_mail_del(name, id); is not called correct. Unfortunately WP shows me no Errors.
Thank you for your help :)
Unfortunately it's not possible to get the name that way, as you guessed, the name is already deleted and there is no 'pre' hook.
You could work arround that by hooking a function to the creation of categories, that adds the name to a different table in the database, togehter with the ID.
And then, if the category gets deleted, you can look the name up there and then also delete it afterwards.
I have some questions concering routing with Codeigniter. What I´m doing now is the following:
$route['articles/(:num)'] = 'articles/view/$1'; // $1 will contain an ID
This means that example.com/articles/123 will work perfectly and load an article with an ID of 123. But I also want to have the possiblilty to add the aticle´s title to the URL (for SEO). Example: example.com/articles/123/article-title
What I want is pretty much the same thing as Stack Overflow: stackoverflow.com/questions/123/the-title
How can I do that?
I´m also wondering how Stack Overflow works. If I go to stackoverflow/questions/111 the title will automatically be added to the url. Is that done with php redirect()?
I have done something similar in the past; I can't find it know but IIRC (it was months ago) You can use a route like you did, and also add a more specific one, like
$route['articles/(:num)/(:any)'] = 'articles/view/$1/$2';
$route['articles/(:num)'] = 'articles/view/$1';
As you can see, both map to the same method, which is kind of "overloaded"; you can make up for a missing parameter by using a default value:
function articles($id,$slug = FALSE)
{ }
and simply ignore the second parameter in your article retrieval.
As for adding the title you can:
have a "slug" field in your database, created when the article is saved. You can use the comfortable url_title($title,'dash',TRUE) function (in the url helper), which takes the $title, uses the dash as separator, and make it all lowercase;
use the above function and convert the title of the article (after you retrieved it from the database) "on-the-fly"; just check on the article() method if the 2nd parameter isn't false and you'll know if you need to create the slug or not;
As for how to show the slug even when using an url without it you can make, as you guessed, a redirect, but since both routes point to the same method it won't change anything for you.
Oh, uhm, beware of loops while calling the redirect, check carefully ;)
I suggest you to create a slug field into your table and complete it with the url you want to use as id.
Let me explain.
You have this table
id
title
slug
when you save an article into your db you can dinamically create a slug, for example:
id: 1
title: My first post
slug: 1-my-first-post
then you can use the slug (in this case 1-my-first-post) ad id for the page, you can call it:
www.example.com/articles/1-my-first-post
obviusly you need to handle it in your db slect
As we discussed on the comments.
You can create a route several times and with different parameters each, like:
$route['articles/(:num)/(:any)']
$route['articles/(:num)']
I would create a function with a redirect, adding or not the title to it.
Hope it helps.
Going a bit mad here... :)
I'm just trying to add CCK fields from a Content Profile content type into page-user.tpl.php (I'm creating a highly-themed user profile page).
There seem to be two methods, both of which have a unique disadvantage that I can't seem to overcome:
'$content profile' method.
$var = $content_profile->get_variables('profile');
print $var['field_last_name'][0]['safe'];
This is great, except I can't seem to pass the currently viewed user into $content_profile, and it therefore always shows the logged in user.
'$content profile load' method.
$account_id = arg(1);
$account = user_load($account_id);
$user_id = $account->uid;
$var = content_profile_load('profile', $user_id);
print $var->field_first_name[0]['value'];
Fine, but now I can't access the full rendered fields, only the plain values (i.e. if the field has paragraphs they won't show up).
How can I have both things at once? In other words how can I show fields relating to the currently viewed user that are also rendered (the 'safe' format in 1)?
I've Googled and Googled and I just end up going round in circles. :(
Cheers,
James
Your content profile load method seems to be the closest to what you want.
In your example:
$account_id = arg(1);
$account = user_load($account_id);
$user_id = $account->uid;
$var = content_profile_load('profile', $user_id);
print $var->field_first_name[0]['value'];
The $var is just a node object. You can get the "full rendered fields" in a number of ways (assuming you mean your field with a filter applied).
The most important thing to check is that you're field is actually configured properly.
Go to:
admin/content/node-type/[node-type]/fields/field_[field-name] to configure your field and make sure that under text processing that you've got "Filtered text" selected.
If that doesn't fix it,try applying this:
content_view_field(content_fields("field_last_name"), $var, FALSE, FALSE)
(more info on this here: http://www.trevorsimonton.com/blog/cck-field-render-node-formatter-format-output-print-echo )
in place of this:
print $var->field_first_name[0]['value'];
if none of that helps... try out some of the things i've got on my blog about this very problem:
http://www.trevorsimonton.com/blog/print-filtered-text-body-input-format-text-processing-node-template-field-php-drupal
When you're creating a user profile page there is a built in mechanism for it. just create a user template file, user_profile.tpl.php.
When you use the built in mechanism you automatically get access to the $account object of the user you are browsing, including all user profile cck fields. You have the fields you are looking for without having to programmatically load the user.
I have a field called profile_bio and am able to spit out any mark up that is in without ever having to ask for the $account.
<?php if ($account->content[Profile][profile_bio]['#value']) print "<h3>Bio</h3>".$account->content[Profile][profile_bio]['#value']; ?>
I've tried themeing content profiles by displaying profile node fields through the userpage before and it always seems a little "hacky" to me. What I've grown quite fond of is simply going to the content profile settings page for that node type and setting the display to "Display the full content". This is fine and dandy except for the stupid markup like the node type name that content profile injects.
a solution for that is to add a preprocess function for the content profile template. one that will unset the $title and remove the node-type name that appears on the profile normally.
function mymodule_preprocess_content_profile_display_view(&$variables) {
if ($variables['type'] == 'nodetypename') {
unset($variables['title']);
}
}
A function similar to this should do the trick. Now to theme user profiles you can simply theme your profile nodes as normal.