Is it possible to "seed" a database like you can in rails? I want to use a seed in combination with an imageobject manager so that I can get records by title.
Based on your comment left on Ingo's answer, you want to add a requireDefaultRecords() method to your page class.
The below is from a recent project and ensures there is a particular user group, but you can do the same with any type of DataObject (e.g. Page).
public function requireDefaultRecords() {
// Make sure there is a readers security group
$group = Group::get('Group')->filter('Code', 'readers')
if ( !$group->exists() ) {
$group = Group::create(array('Title' => 'Readers'));
$group->write();
}
}
This function is run on all DataObject classes when you do a build.
You can set the default values of your page $db variables by setting the $defaults array.
class Page extends SiteTree {
public static $db = array(
'Title' => 'Text',
'Description' => 'Text'
);
public static $defaults = array(
'Title' => 'Default Title',
'Description' => 'Default Description'
);
...
}
Not quite sure what you mean by "seed" in this context. There's a "data-generator" module which writes random data with educated guesses on the ORM column types.
Related
I have a SilverStripe site with some code to display a search form. The for allows you to search for something based on 3 things. Problem is, I'm not sure how to get the results to display correctly on a separate page.
My code:
class InstitutionSearchPage extends Page {
}
class InstitutionSearchPage_Controller extends Page_Controller {
private static $allowed_actions = array(
'Search'
);
public function Form() {
$fields = new FieldList(array(
DropdownField::create('DegreeType', 'Degree', QualificationType::get()->filter('ParentID', 0)->map()),
DropdownField::create('Course', 'Course', Qualification::get()->map()),
DropdownField::create('City', 'City', City::get()->map()),
));
$actions = new FieldList(array(
FormAction::create('Search')->setTitle('Find a College')
));
$validator = ZenValidator::create();
$validator->addRequiredFields(array(
'DegreeType' => 'Please select a Degree',
'Course' => 'Please select a course',
'City' => 'Please select a city',
));
$form = new Form($this, 'Search', $fields, $actions, $validator);
$form->setLegend('Criteria')->addExtraClass('form-horizontal')->setAttribute('data-toggle', 'validator');
// Load the form with previously sent data
$form->loadDataFrom($this->request->postVars());
return $form;
}
public function Search() {
return array('Content' => print_r($this->getRequest()->postVars(), true));
}
}
It seems to be displaying results on the same page but gives me a bunch of weird data. For example, I got this when I tested the form: Array ( [DegreeType] => 53 [Course] => 1 [City] => 1 [SecurityID] => 02718d0283e27eeb539eff19616e0b23eadd6b94 [action_Search] => Find a College )
The result is supposed to be an organized list of colleges (along with other data).
I guess that what you are seeing is expected behavior, as the form will post to the Search function, and that one is just returning a print_r of an array with the post vars which will be picked up by the template.
Otherwise, there are a lot of things not corresponding with the Silverstripe default way to handle forms. Please take a look here and change your form accordingly: https://docs.silverstripe.org/en/3.4/tutorials/forms/
For example, give the form the same name as your function (or in your case, change the function name to the form name). Then implement the action function.
I'm trying to understand and get around this whole form collection thing, but the documentation isn't really expansive and I just can't find out how to do some specific things I need.
I will refer to the example in the official manual to explain what i need:
When the collection is created, you get to use a custom fieldset as target:
$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'categories',
'options' => array(
'label' => 'Please choose categories for this product',
'count' => 2,
'should_create_template' => true,
'template_placeholder' => '__placeholder_:',
**'target_element' => array(
'type' => 'Application\Form\CategoryFieldset',
)**,
),
));
However, I need to pass an argument to the specific fieldset's constructor, in my case a translator instance in order to be able to translate within the fieldset.
class CategoryFieldset extends Fieldset
{
public function __construct($translator)
}
Fieldset's label: as you can see in the example, the collection outputs all the copies of the fieldset with the same specified label "Category". I would need, instead, to have that label numbered, to show "Category 1", "Category 2" etc. based on the collection's count. Is this even possible?
Thanks for your help!
I checked source of the Collection. The Collection just clones target_element. My solution is simple and works:
class CategoryFieldset extends Fieldset implements InputFilterProviderInterface
{
static $lp = 1;// <----------- add this line
public function __clone() //<------------ add this method
{
parent::__clone();
$oldLabel = $this->elements['name']->getLabel();
$this->elements['name']->setLabel($oldLabel . ' ' . self::$lp++);
}
For the first, do not pass translator to the Fieldset, but use translator outside of the Fieldset. Get the values first, from the form, translate them, then set them back into the form. The bonus is that you keep your form and your translator logic separate.
For the second, use $form->prepare() and then iterate over the Collection.
$form->prepare(); //clones collection elements
$collection = $form->get('YOUR_COLLECTION_ELEMENT_NAME');
foreach ($collection as $fieldset)
$fieldset->get('INDIVIDUAL_ELEMENT_NAME')->setLabel("WHATEVER YOU WANT");
Example:
/*
* In your model or controller:
*/
$form->prepare();
$collection = $form->get('categories');
foreach ($collection as $fieldset)
{
$label = $fieldset->get('name')->getLabel();
$translatedLabel = $translator->translate($label);
$fieldset->get('name')->setLabel($translatedLabel);
}
We're running into a small code-design smell with symfony and our forms. It is not a problem per se, but makes me wonder if we could attain our goals any other way.
For the sake of simplicity, let me briefly explain a setup: let "Product" be an entity that represents a product in a database, meant to be sold in an online store. Since the online store is designed to have several languages in it, every single bit of information that could be related to a language is in the entity "Product_descriptions" that is related in a manyToOne fashion to the "Product". Finally we have designed a "Language" entity, representing every single language the user can see the store in.
As you can imagine, the code is pretty standard stuff:
class Language
{
private $language_id;
private $language_name;
private $language_code;
//Some other stuff.
};
class Product
{
private $product_id;
private $product_reference;
private $product_weight;
private $product_descriptions; //As an arrayCollection of "Product_description" objects.
//Some other stuff.
};
class Product_description
{
private $product_description_id;
private $product_name;
private $product_long_description;
private $product_short_description;
private $product; //A reference to the Product itself.
private $language; //A reference to the language this is meant to be seen in.
};
Okay, now for the problem itself. The setup, as expected, works wonderfully. It is in the backend where the smell resides.
To create new products we have designed a symfony form Type. In the same form we would like to be able to set all the product information as well as the information for every possible language. The smell comes in when we need to feed all possible "Language"s to the form type, check if a "Product_description" exists for a "Language" and "Product", show the empty text field (in case it does not exist) or the filled field... Our solution requests that a repository for all languages is injected into the form . Let me show you how it goes (please, take into consideration that this is not the real code... something may be missing):
class ProductType extends AbstractType
{
private $language_repo;
public function __construct($r)
{
$this->language_repo=$r;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('product_name', 'text')
->add('product_code', 'text');
$product=$builder->getData();
//We retrieve all languages here, to check if an entry for that
//language exists and show its data.
$languages=$this->language_repo->findAll();
foreach($languages as $key => &$lan)
{
//Here we look for existing data... This will return null if there's none.
$product_description=$product->get_description_for_language($lan);
$default_name=$product_description ? $product_description->getProductName() : '';
$default_long=$product_description ? $product_description->getProductLongDescription() : '';
$default_short=$product_description ? $product_description->getProductShortDescription() : '';
//Here we manually create the name_#language_id# form data... That we will retrieve later.
$builder->add('name_'.$lan->getLanguageId(), 'text', array(
'label' => 'Name for '.$lan->getName(),
'mapped' => false,
'data' => $default_name))
->add('long_'.$lan->getLanguageId(), 'text', array(
'label' => 'Name for '.$lan->getName(),
'mapped' => false,
'data' => $default_long))
->add('short_'.$lan->getLanguageId(), 'text', array(
'label' => 'Name for '.$lan->getName(),
'mapped' => false,
'data' => $default_short));
}
$builder->add('save', 'submit', array('label' => 'Save data'));
}
//And some other stuff here.
}
As you can see, we are manually setting some data keys that we need to retrieve later in the controller. The setup works, of course. Any new language will yield an empty form field. Any existing language shows the related information.
Now for the controller, this gets messier even... When we're submitting the form we go like this:
private function process_form_data(Form &$f, Product &$item, Request &$request)
{
//Find all languages...
$em=$this->getDoctrine()->getManager();
$languages=$em->getRepository("MyBundle:Language")->findAll();
//Get submitted data for that language..
foreach($languages as $key => &$lan)
{
$name_language=$f->get('name_'.$lan->getLanguageId())->getData();
$long_language=$f->get('long_'.$lan->getLanguageId())->getData();
$short_language=$f->get('short_'.$lan->getLanguageId())->getData();
//Check if the language entry exists... Create it, if it doesn't. Feed the data.
$product_description=$product->get_description_for_language($lan);
if(!$product_description)
{
$product_description=new Product_description();
$product_description->setLanguage($lan);
$product_description->setProduct($product);
}
$product_description->setName($name_language);
$product_description->setLongDescription($long_language);
$product_description->setShortDescription($short_language);
$em->persist($product_description);
}
//Do the product stuff, persist, flush, generate a redirect...Not shown.
}
It works, but seems to me that is not the "symfony" way of doing things. How would you do this?. Have you found a more elegant approach?.
Thanks a lot.
I think you should revisit the way you translate the entities...
An existing way is to use the DoctrineExtensionBundle, translatable to be precise...
You'll find more info here :
https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/translatable.md
Here is an extract to see how it can work :
<?php
// first load the article
$article = $em->find('Entity\Article', 1 /*article id*/);
$article->setTitle('my title in de');
$article->setContent('my content in de');
$article->setTranslatableLocale('de_de'); // change locale
$em->persist($article);
$em->flush();
( now the article has a german translation )
Im back to professional programming after 5 year late, so i have learn from beginning ;)
So i chose Kohana framework for my first framework i try to bulid first application now and i have a small problem, lets begin.
I use Kohana ORM and Auth module, as you know default Auth module user table have default fields (username, password, lastlogin) i try to extend User by:
Creating new table (user_additionals)
Creating new model (User_Additional)
Model look like this: http://pastie.org/private/412jflfoifyqs46uaxmga - Nothing special. Everything will be okay, i like easy reference like this: $user->additional->firstname etc.
At the moment i have bulid admin panel (admin can edit every user) and... every field. I have 10 fields like firstname, lastname, birthdate presented as form (filled form - placeholder loaded by template assign) and here is my small problem:
I want to give admin possibility to edit one from much fields, if admin need to edit user signature or something else - he edit one field from a lot fields available and click "Submit" it's easy - one form have been updated.
But, if i try use something like this:
$edit = ORM::Factory('User_Additional')->values($_POST); I get validation Exception (which be catched but, here are validation error - model required all fields to be !empty... (By validation rules)
I use temporary solution, but im a perfectionist and i want to create good code from begining, so here you can find my code: http://pastie.org/private/axtwxbt66gtvcwiv97hvlq
My solution start at line 29 (link above).
So my question is:
*How to make exceptions from Validation in cases like this?? *
*How make exceptions from validation for example for action /admin/edituser/ is it possible? *
*How do i can do my model code better? Thanks for any suggestions which can help me *
Thanks!
Validation will only run on "non-empty" fields, unless you specify the "not_empty" rule.
So basically, you could do the following: (when you need to enforce the "non emptiness" of a field)
class Model_User_Additional extends ORM
{
protected $foreign_key_suffix = 'user_id';
protected $_primary_key = 'user_id';
public function enforce_rules(array $fields)
{
$validation = Validation::factory($this->_object);
foreach ($fields as $field)
{
$validation->rule($field, 'not_empty');
}
return $validation;
}
public function rules()
{
/* Removed all not_empty */
return array(
/* 'user_id' => array(
array('not_empty'),
), */
'firstname' => array(
array('text'),
),
'lastname' => array(
array('text'),
),
'birthdate' => array(
array('date'),
),
'postprice' => array(
array('decimal'),
),
'articleprice' => array(
array('decimal'),
),
'phonenumber' => array(
array('phone'),
),
/* 'active' => array(
array('not_empty'),
), */
/* 'lastupdate' => array(
array('not_empty'),
), */
'info' => array(
array('text'),
),
);
}
public function edit_user($values, $expected, $extra_validation = NULL)
{
return $this->values($values, $expected)->update($extra_validation);
}
}
the usage would be:
$user = ORM::factory('User_Additional');
$user->values(array('field' => 'value'))->create($user->enforce_rules(array('user_id')));
/* and the same for updating */
$user->set('info', $_POST['info'])->update($user->enforce_rules(array('info')));
I am wondering how I could use data from a Model B while I am validating Model A, here to check if an entered Banknumber is a correct one:
My Users specify their bankaccount during the registration. E.g. the "banknumber". I am validating this the normal way in my user.php model
var $validate = array(
'banknumber' => array(
'minLength' => array(
'rule' => array('minLength', 8),
'message' => '...',
'required' => true,
),
Now I want to know if the entered Banknumber is a real one, so I got a table "Banks" in my DB with all real Banknumbers, and I am using some own validation functions which I specify in app_model.php.
function checkBankExists($data) {
if (!$this->Bank->findByBanknumber($data)) {
return false;
} else {
return true;
}
}
But this is never working, because while I am validating the User-Model, I can only use this one in an app_model - function, accessing it with $this->name or so... a $this->Bank is NOT possible, I get:
Undefined property: User::$Bank [APP\app_model.php
Call to a member function findByBanknumber() on a non-object
Is there ANY way to import/access other models in a function in app_model.php?
Thank you!
ClassRegistry should generally be used instead of AppImport as AppImport only loads the file, rather than registering it properly, cake style.
Using the example above.
$bnk = ClassRegistry::init('Bank');
$bnk->findByBanknumber($data);
you can import your model, create instance of it and use it as you like:
App::import('model','Bank');
$bnk = new Bank();
$bnk->findByBanknumber($data);