Hello I have several troubles with HABTM and Form.
I am using CakePHP 2.3 and PHP 5.3.3
This is my code situation for the understanding of the scenario: Albums and Singles related with a HABTM relation. Albums cointains many Singles and Singles belongs to many Albums.
Model Album.php ( don't care about raisins: is my personal plugin for managing images )
App::uses('Raisin', 'Raisins.Model');
App::uses('PastryBehavior', 'Raisins.Model/Behavior');
class Album extends AppModel
{
public $name = 'Album';
public $actsAs = array(
'Tags.Taggable',
'Utils.Sluggable' => array(
'label' => 'title'
),
'Raisins.Pastry',
'SrEngine.Indexable',
'SrEngine.HitCounter'
);
public $hasOne = array(
'Image' => array('className' => 'Raisins.Raisin')
);
public $hasAndBelongsToMany = array(
'Single'
);
public $belongsTo = array(
'Users.User'
);
}
Controller AlbumsController.php ( edit part )
$this->request->data = $this->Album->read(null, $id);
$this->set('singles',$this->Album->Single->find('list'));
View
<?php echo $this->Form->input('Single',array('class' => 'span12','label' => 'Singoli' ) ); ?>
In the view I see the select populated from the values coming from the controller singles set, but the selected item, previously saved, is not highlighted.
Consider that in the database the habtm table contains the correct row populated with the data saved.
I read , somewehere in the middle, that some previous version of cake, prior to 2.3, used to have some problem related to habtm form select input.
I am very stuck at this because I have almost tried every known workaround without sorting any positive effect.
Any help, hint would be very appreciated.
Thanks in advance
Rudy
Related
im new in WordPress i wan to change my WooCommerce check out form postcode to dropdown box.. im using the solution given online but it's not work on my page.. pls help me.
here the code i put inside flatsome child function file.
add_filter( 'woocommerce_default_address_fields' , 'customize_postcode_fields' );
function customize_postcode_fields( $adresses_fields ) {
$adresses_fields['postcode']['type'] = 'select';
$adresses_fields['postcode']['options'] = array(
'' => __('Select your postcode', 'woocommerce'),
'option_1' => 'Choice 1',
'option_2' => 'Choice 2',
'option_3' => 'Choice 3'
);
return $adresses_fields;
}
and the result that i get after apply the code
my display after apply
I have updated my post from earlier which may make it easier for you to interpret and put into your code (I changed it from my last answer, as what I published wont work) - the main difference is putting the array into a variable, to make it easier to change in the future. I have done some research as well and found that the information you provided was the best option for this (so I just cleaned it up a bit). However, if you could also inspect/view-source on your code
function wpe_0987_customize_postcode_fields( $postcode_field ) {
$options = array(
'' => __( 'Select...', 'woocommerce' ),
'choice_1' => 'choice_1',
'choice_2' => 'choice_2',
'choice_3' => 'choice_3',
'choice_4' => 'choice_4'
);
$fields['billing_postcode']['type'] = 'select';
$fields['shipping_postcode']['type'] = 'select';
$fields['billing_postcode']['options'] = $options;
$fields['shipping_postcode']['options'] = $options;
return $fields;
}
For Shipping Field Only!
add_filter( 'woocommerce_shipping_fields' , 'wpe_0987_customize_postcode_fields' );
For Shipping and Billing Fields
add_filter( 'woocommerce_default_address_fields' , 'wpe_0987_customize_postcode_fields' );
Also, when you create custom functions, don't forget to put a custom prefix at the start. At the moment you have 'customize_postcode_fields', you should find a series of letters/numbers or something unique to you to ensure that it doesn't clash with any other theme/plugin - eg: 'random123_customize_postcode_fields' and use that prefix on all custom functions you create in that project.
Update:
Do an 'inspect' or 'view source' on the page, check to see what the 'name' of the postcode form is, and update it with one of the two that I have provided above (shipping_postcode, billing_postcode).
i have the following models: Student,Form,FormsStream.
the student model have foreign keys to both models. Student.form_id is a foreignKey to the form table,Student.stream_id is foreign to the FormsStream table. i already have entries for both forms and formsStreams.
to add the student Form and Stream i select both from respective drop-down list. i already created the drop-down list.
my Problem.
previously i was able to save/edit student form and stream but after making those fields foreign keys to be selected i cant save new or edit those fields. i use cakePHP 2.5.4.
however i can do that in SQL.
i cant seem to figure out the problem with the select lists.
my method to get the forms from the database
public function loadStudentForm() {
$this->loadModel('Student'); //load the associated models
$this->loadModel('Form');
$forms = $this->Form->find('list',array('fields' => 'form_name')); //get the forms from the database
$this->set('forms',$forms);
}
method to get streams from database
public function loadStreams() {
$this->loadModel('FormsStream');
$streams = $this->FormsStream->find('list',array('fields' => 'stream_name'));
$this->set('streams',$streams);
}
student add method
public function addStudent() {
if ($this->request->is('post')) {
$this->loadStudentForm();
$this->loadStreams();
$this->Student->create();
if ($this->Student->save($this->request->data)) {
$this->Session->setFlash(__('New student record added to the database.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The student could not be added.Please ensure all fields are correctly entered'));
}
}
}
the extract from add.ctp for selecting student form and stream
echo $this->Form->input('forms',array('label' => 'Form'));
echo $this->Form->input('streams',array('label' => 'Class stream'));
so far printing contents of validateErrors shows this:
array(
'Student' => array(
'form_id' => array(
(int) 0 => 'You must select the form'
),
'stream_id' => array(
(int) 0 => 'You must choose a stream'
)
),
'Form' => array(),
'FormsStream' => array()
)
i've even tried to intercept the request in burpsuite and i can see the form and stream id are passed.just dont get why they are empty in the above array
_method=POST&data%5BStudent%5D%5Badmission_no%5D=1002&data%5BStudent%5D%5Bfirst_name%5D=peter&data%5BStudent%5D%5Bmiddle_name%5D=per&data%5BStudent%5D%5Blast_name%5D=pee&data%5BStudent%5D%5Bgender%5D=Male&data%5BStudent%5D%5Bdate_of_birth%5D%5Bmonth%5D=11&data%5BStudent%5D%5Bdate_of_birth%5D%5Bday%5D=05&data%5BStudent%5D%5Bdate_of_birth%5D%5Byear%5D=2014&data%5BStudent%5D%5Bjoin_date%5D%5Bmonth%5D=11&data%5BStudent%5D%5Bjoin_date%5D%5Bday%5D=05&data%5BStudent%5D%5Bjoin_date%5D%5Byear%5D=2014&data%5BStudent%5D%5Bforms%5D=1&data%5BStudent%5D%5Bsstrong texttreams%5D=1
The input must be form_id and stream_id. Even if you set them with plural form cakephp recognize it in the following form.
echo $this->Form->input('form_id',array('label' => 'Form'));
echo $this->Form->input('stream_id',array('label' => 'Class stream'));
I'm writing my first Cake app and trying to set up my first deep association. It's very nearly working but I have a couple of small issues.
The first part of my app is a customer database. A customer has many addresses, and an address has many contacts. The data structure returned contains duplicated data from the address model.
Here's the 3 models:
class Customer extends AppModel {
public $hasMany = 'CustomerAddress';
}
class CustomerAddress extends AppModel {
public $belongsTo = 'Customer';
public $hasMany = 'CustomerContact';
}
class CustomerContact extends AppModel {
public $belongsTo = 'CustomerAddress';
}
In my customer controller, I want to get associated addresses and their contacts. I can do this by setting recursion on the find command:
CustomerController.php
public function find( $id = NULL) {
$this->set('customers', $this->Customer->find('all', array( 'recursive' => 2)));
}
And this works brilliantly, with one caveat. The data structure returned looks like this:
array(
'Customer' => array(
'id' => '46',
....
),
'CustomerAddress' => array(
(int) 0 => array(
'id' => '35',
'customer_id' => '46',
.....
'Customer' => array(
'id' => '46',
.....
),
'CustomerContact' => array(
(int) 0 => array(
'id' => '29',
'customer_address_id' => '35',
.....
)
)
)
)
)
At a glance, this looks fine, and for all intents and purposes works as the data is formatted how you'd expect. But, CustomerAddress also contains a Customer object, which is a duplicate of the top level Customer object. I assume this is because recursion is working with the belongsTo on the address model.
I've tried setting the recursion to 1, but then I only get the address not the contact. I've tried setting the recursion in the address model, but that doesn't seem to effect the find at the Customer level.
It's not a huge issue, just worried about future performance issues and unnecessary calls to the database.
This is one of the reasons why it is discouraged to use the recursive setting. Most turn off recursion (set it to -1) then use the Containable behavior to get associated models instead. Make sure in AppModel, there is the line
public $actsAs = array('Containable');
Then, to get your data:
$this->Customer->find('all', array('contain' => array('CustomerAddress' => array('CustomerContact'))));
hi I am new in zend framework Basically I want to populate company name list from the database. Actually I have doen it but i want to also populate its id in option box
example
select
option value='1'> tcs option
select
this is my code
Application_Form_Clientcompanyform extends Zend_Form
$company_list = new Application_Model_Clientcompany;
$showlist = $company_list->companyNameList();
$list=array();
$id=array();
foreach($showlist as $key => $value)
{
$list[]=$value['companyName'];
$id[]=$value['id'];
}
$this->addElement('select', 'companyName', array(
'required' => true,
'filters' => array('StringTrim'),
'style' => array('width:103px'),
'multiOptions' => $list,
'decorators'=>Array(
'ViewHelper','Errors'
but Now i want to set the value in option in select box width $id from database
$companyName = new Zend_Form_Element_Select('companyName');
$companyName->setRequired(true);
$companyName->addFilter('StringTrim');
$company_list = new Application_Model_Clientcompany;
$showlist = $company_list->companyNameList();
//add selections to multioption, assumes object...change notation if using array
foreach($showlist as $company) {
$name = ucfirst($company->name);
$companyName->addMultiOption($company->id, $name);
}
$this->addElement($companyName);
I know I changed the syntax style, I just find it easier to keep things straight this way.
Everything else you may need is in http://framework.zend.com/manual/1.12/en/zend.form.html, get used to using the reference and the the api for the framework, they really help.
I am using Zend-Framework in my project. I made a login form using the Zend Form that contains the User Id and Passwords fields with a submit button. Everything is working fine in the login form.
How do I add two hyperlinks inside the login form that is one for the Sign-Up and other for the Forget Password?
I've faced the same problem before, and solved it by creating a custom Zend_Form_Element_Html, as follows:
class Zend_Form_Element_Html extends Zend_Form_Element_Xhtml {
/**
* Default form view helper to use for rendering
* #var string
*/
public $helper = 'formNote';
public function isValid($value, $context = null) {
return true;
}
}
So, in your form you just have to do the following:
$tag = new Zend_Form_Element_Html('forgetPassword');
$tag->setValue('Forgotten your password?');
$this->addElement($tag);
Hope this helps!
In your viewscript file where you print the form, e.g. login.phtml
echo $this->form;
you can specify any other html markup, e.g. links
echo "<p><a href='".$this->url ( array ('controller' => 'authentication',
'action' => 'lostPW' ) )."'>
Lost pw </a></p>";
So you actually do not write it in the form itself but in the view script where you echo the form.
Try this:
$passwordElement->setDescription('Forgot password?');
$passwordElement->getDecorator('Description')->setOption('escape', false);
Description decorator will add this text beside your field.
You can use Zend_Form_Decorator_ViewScript
Or, create a custom Zend_Form_Element to render HTML elements or ViewScript.
For only decorators use directly in the form try:
$this->addElement(new Zend_Form_Element_Note(array(
'name' => 'forgotten',
'value' => __('Forgot your password?'),
'decorators' => array(
array('ViewHelper'),
array('HtmlTag', array(
'tag' => 'a',
'href' => $this->getView()->url(array(
'remind'
))
)),
)
)), 'forgotten');