How to create 0..1 to many relationship in octobercms? - database

I have a table category it has a self relation to have belongs_to simply to be able to create subcategories and assign them to previously created categories and now i need to be able to either assign the new category to another (super) category or to assign it to none.
my model is :
public $belongsTo = [
'category' => ['plugin\pdf\models\Category']
];

This is found in the documents under backend -> forms. Look for the relation under widget fields. The option you is called emptyOption and it will be added in your fields.yaml file.
You would add the emptyOption like so to model's fields.yaml.
field:
label: Field Label
nameFrom: name
descriptionFrom: description
span: auto
type: relation
emptyOption: None

Related

I created a custom User, how do I add them to a group?

I created a Custom User midway through my project, Called OUser, in an app called accounts. I have some existing groups defined in the default AuthUserGroups. How do I add OUsers to a group? When I try, I get an error, Invalid column name 'ouser_id'. What do I need to do to allow OUsers to be added to my predefined groups? Do I have to create a custom group model to put my OUsers? Is there anyway that I can over tell Auth_User_groups that it should be looking to OUsers?
accounts.admin
class OUserAdmin(BaseUserAdmin):
# The forms to add and change user instances
form = UserAdminChangeForm
add_form = UserAdminCreationForm
# The fields to be used in displaying the User model.
# These override the definitions on the base UserAdmin
# that reference specific fields on auth.User.
fieldsets = (
('Personal info', {'fields': ('first_name', 'last_name', 'email',)}),
('Permissions', {'fields': ('is_superuser', 'is_active', 'is_staff',)}),
)
# add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
# overrides get_fieldsets to use this attribute when creating a user.
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('username',)}
),
)
search_fields = ('username',)
ordering = ('username',)
filter_horizontal = ()
list_filter = ('is_superuser',)
admin.site.register(OUser, OUserAdmin)
'''
There are also other dependencies on things, such as Auth_Group_permission, but I assume that it will be a similar fix to switching Auth_Group to using OUser.
How would I make groups contain references to Ouser. I have tried just swapping the Foreign key of the user_id in AuthUserGroups to contain a reference to OUser, but that didn't work. Maybe I missed something?
Thanks for the help!
AuthUserGroups is a table which holds the ManyToMany relationships between User and Group. It has foreign key constraints to these models. You can't have a field which has a foreign key relationship to both OUsers and User
You would need to create a different ManyToMany relationship form OUsers to Group that goes through a separate table.
Here is an example on to add it to the OUser model
from django.contrib.auth.models import Group
class OUsers(models.Model):
...
groups = models.ManyToManyField(Group)
Docs on ManyToMany fields

Drupal7: real time link selected field to a text field in Drupal7

I'm creating a webform to build up a subscription form, on Drupal 7.65
Goal
What I need to do is: to select a role from a list, and automatically to display the associated name of that role, in a text field.
As I said, the name should be displayed into a not modifiable text field just below it.
Suppose valid, the following list (key => value)
Field: Department
business_manager|Business Manager
hr_consultant|Human Resources
training_developer|Training Developer
and from the time going on, the associated names, are respectively
Options can appear into text field hr_business_partner
Steve Abc
Gertrude Def
Sven Hgj Klm
Thus when the trainee selects "Human Resources", the name of "Gertrude Def" should appear into the text field below the select one.
I've attached a mokup to better understand what I do need.
IMPORTANT
I can't put the names into the list as value, because the association can change but old records should keep the previously registered associations
You can use hook_form_alter() and add a new select field with the paired key value list you need to the webform. And then use javascript to update which field value gets shown in the HR Business Partner field on change, which by the way would also need to be added via your hook_form_alter. You could use a taxonomy to maintain a list of Departments/Business partners which would populate your department and business partners.
Write some javascript to dynamically update your original fields not added through the form_alter, on change. I would suggest making two textfields in your webform components which will hold the value from your form alter added fields. So that these values selected by the user gets saved in your form.
function MODULENAME_form_alter(&$form, &$form_state, $form_id) {
if($form_id == "webform_client_form_####"){
$form['#attached']['js'] = array(drupal_get_path('module','MODULENAME') . '/js/webform.js');
$form['hr_dept'] = array(
"#type" => "select",
"#options" => array("business_manager"=>"Business Manager", "hr_consultant"=>"Human Resources"),
);
$partners = taxonomy_get_tree(#); //the VID of the taxonomy
$list = array("0"=>"None"); //first option
foreach($partners as $tid => $partner){
$list[$partner->tid] = $partner->name;
}
$form['hr_partner'] = array(
'#type' => 'select',
'#options' => $list,
);
}
}
In your javascript file, /js/webform.js you can include all your logic to check for which value is selected on the Department field and then display the correct value in the Partners fields. At the same time, updating the original fields you've added as textfields in the webform components UI.

How to create objects that have relationships when using Backand BaaS?

I am using Backand BaaS for my ionic app. My model consists of two tables:
1. Users
2. Trips
A one-to-many relationship has been established between 'Users' and 'Trips'.
'Users' has a collection field that is a collection of 'Trips' and 'Trips' has a owner field that is an object of 'Users'.
What is the correct way to create an object so that upon creating I am assigning the correct owner ('Users' object) to 'Trips' collection field ?
When POSTing a new Trip use this for the Trip Object
{
tripName:"asdf"
owner:1
}
In the users object it will look like this
{
id:1
fName:"david"
trips:[{__metadata: {id: 1}, tripName: "asdf"}]
}

Symfony Form customize array for entity type

In my Symfony2.6.6 project I have a category entity. I don't know how to say that properly but use the doctrine tree extension to create a tree with the category entity like:
Category 1
Child Category 1
Child Category 2
Category 2
Child Category 3
...
Now when creating the form type for creating a new Category I want to customize the array of the parent field.
The code I use
$builder->add('parent', 'entity', array('class' => 'AcmeBlogBundle:Category', 'property' => 'title'));
generates just an array with all the title as expected. But I want the array values to append a '-' for every level of the tree. Category 1 for example would be '- Category 1' and Child Category 1 would be '-- Child Category 1'. How can I do that?
Sorry if this has been asked before, I don't know how to search for that.
Okay, let's say you have property getLevel() which would return your current item's level. Then in your Category entity file, create a simple method, for example getIndentedTitle thar would return your pre-defined category based on its level.
public function getIndentedTitle() {
return sprintf(
'%s %s',
str_repeat('-', $this->getLevel()),
$this->getTitle()
);
}
The following code will add as many "-" symbols as your current category level and will append its title next to it.
Then in your form builder, you have to specify to use that method:
$builder->add('parent', 'entity', array('class' => 'AcmeBlogBundle:Category', 'property' => 'indentedTitle'));
More info on str_repeat.
Hope you got the idea.

Cakephp FormHelper Create select box

i am cakephp beginner.
My Employee Model,
class Employee extends AppModel {
var $belongsTo = array(
'Department'=>array(
'className'=>'Department',
'foreignKey'=>'department_id',
'conditions'=>null,
'fields'=>null
)
);
blah--
now in employee add.ctp i want to create a select box which list all the department.
i was going through official cakephp 2.1 documentation (here)
it tells me to add
$this->set('departments', $this->Employee->Department->find('list'));
in my controller..
i have no idea to put in which controller ? is it in EmployeesController or DepartmentsController? and in which action of controller?
view to create select box (in add.ctp)
echo $this->Form->input('Department');
you were almost correct - only a minor glitch:
echo $this->Form->input('department_id');
you need to name the fields as they are in the database.
and if it is a BelongsTo relation than there should be a department_id foreign key in your employees table.
PS: cake knows that if you pass down $departments that this array will need to be the options for this form field. so no additional configuration necessary!
// in your add action at the very bottom
$departments = $this->Employee->Department->find('list');
$this->set(compact('departments'));

Resources