Filed under: php

YII Relation Quiries in Forms

Introduction

I’ve inherited this application built totally on Yii. It’s a administration site that handles the management of some of our tools and I’ve been tasked with setting up user management.

I just got user auth working with salted passwords. Which works wonders and was (looking back) easier then thought to implement. Now I’m tasked with setting up user role management.

My schema looks like:

User
--------------------------
UserID, Name, Email, Password

UserInRoles
-----------------
PK, userID, RoleID

Roles
-------------------
roleID, Name, AccessLevel

When you build out your models (I used Giix to generate it) you can override the default relations() function to create relations between tables. Great, I did that. Then I ran the CRUD for Giix. I got user in roles, and a few other junk tables I really didn’t need.

What I needed was to take the user from the user table, associate it with a role I select and post that into the userinroles table. Easy? So, I thought.

How to:

1. Setup relations in your models In terms of how things are laid out, this is your knowledge of your app comes in. My user stuff related to userinroles which related to roles. I defined these relations in each class

2. Run Giix to create the CRUD interface

3. Now go into the view you want to be the central entry point for managing all these relations, for me it was the _form.php user view.

3 a.  In the _form.php it generated.

<label>Tools Access</label>
<?php echo $form->checkBoxList($model, 'toolaccesses', GxHtml::encodeEx(GxHtml::listDataEx(Toolaccess::model()->findAllAttributes(null, true)), false, true)); ?>

3 b. So this automatically generated one out of 3 of my relations for this view, so I duplicated it twice, updated Toolaccess:: to reflect the controllers Role & tool, and the , 'toolaccesses' to reflect the model I wanted to update and tested it. It renders the form correctly but post doesn’t work. Well you need to update the user controller ( the controller for this view )

4. That’s easy enough, in my user controller update, I check if the userinroles post has been defined, if so iterate over that array and create a new class, pass in my attributes and update the record.

if(isset($_POST['User']['userinroles'])){
    foreach($_POST['User']['userinroles'] as $role){
        $userInRoles = new Userinroles:
        $userInRoles->setAttribute('userid', $id);
        $userInRoles->setAttribute('roleid', $role);
        $userInRoles->save();
    }
}
 
4 A. Just duplicate for actionCreate() and actionDelete(), with some modifications but you now have a basic way to add roles to a user.

 It may be smart in the update method to check if the user already has this role, if so, don’t do an update, if the user has new roles but not old ones, delete the old ones. But this gives you the basic structure to get you going.               

Custom background images using Wordpress

Wordpress as a CMS

Wordpress is an excellent system to build your website, blog or even portfolio on. Almost all of my projects these days are for using Wordpress as a CMS. Not only can Wordpress do blogging very well, but it can also do content management really well. Today I'm going to introduce you to the custom field. 

The Custom Field 

The custom field has been around Wordpress since version 2. It allows you to customize posts & pages with content content that can exist in areas other then the primary post area. For example you can use it to display custom HTML in sidebars based on what page it is just by specifying the correct Custom Field name in your Post or Page. 

The Example 

A really good example of this is on a recent project I needed to have a unique background image with a default fallback image in place just incase we didn't set a new background image. There are many potential solutions to this problem: 

  • Set an ID of the div relative to the page ID value & create a custom CSS stylesheet with each background image
  • Setup a widget/sidebar area & insert a html style on the element 
  • Setup a widget/sidebar area & under lay an image & use z-index for positioning

Those would have been very complex, non-user friendly & hard to maintain; so I opted for the more user friendly experience: use Custom Fields. Custom fields are great because you can define anything you want or need within that field that is specific to just that page or blog post. But it provides you with a nice interface for managing content. 

The Code

It's really, really easy to get going on Custom Fields in Wordpress. In your page file all you need to do is define a name & then call it in your page or post. 

Here's an example of my code: 

<?php

// Showing the custom image defined on the page on admin 

if(get_post_meta($post->ID, "backgroundImage", true) != "") {

$format = 'style="background:url(%s);background-repeat:no-repeat;"';

printf($format,get_post_meta($post->ID, "backgroundImage", true));

}

?>

Let me break it down for you: 

  • Simple control structure to check for the existence of the get_post_meta data
  • The get_post_meta returns the data found with the page/post ID of argument backgroundImage, if nothing's found it returns a false & terminates the statement. 
  • $format is setting up a PHP format string, with the %s esentially a veriable we're going to use later
  • the printf calls the $format & the backgroundImage and formats it into a string

Doing this ensures that:

  1. I have a fall back for when something isn't defined. 
  2. I override any css by placing this as an inline style vs a css stylesheet style, ex <div //PHPstatement> </div> 
  3. The format string insures that I get the correct placement of the backgroundImage link every time the script is run when the page is built. 

Now to use this, all you need to go to is Post (Or Page) > Your page or Create New > Under the body of where you enter the content is a section for custom fields. Just plugin backgroundImages & the url, then you are golden! 

Resources