I am using latest(2.1.1)version of cakePhp.
I was trying to implement ajaxHlper(http://www.cakephp.4uk.pl/) . but stuck in between.
<head>
<script type="text/javascript">
$(document).ready(function(){
$("#EmployeeAddForm").validate();
});
</script>
</head>
<div class="employees form">
<?php echo $this->Form->create('Employee');?>
<fieldset>
<legend><?php echo __('Add Employee'); ?></legend>
<?php
echo $this->Form->input('first_name');
echo $this->Form->input('last_name');
//echo $this->Form->input('age');
echo $this->Form->input('age', array('class' => 'required number'));
echo $this->Form->input('sex');
echo $this->Form->input('Adress.first_line');
echo $this->Form->input('Adress.second_line');
echo $this->Form->input('Adress.city');
echo $this->Form->input('Adress.state');
echo $ajax->autoComplete('Department.name', '/ajax/autoComplete')
?>
</fieldset>
<?php echo $this->Form->end(__('Submit'));?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('List Employees'), array('action' => 'index'));?></li>
<li><?php echo $this->Html->link(__('List Adresses'), array('controller' => 'adresses', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New Adress'), array('controller' => 'adresses', 'action' => 'add')); ?> </li>
</ul>
This is my add.ctp file where Department.name I am trying to autocomplete using ajaxHelper.
In my EmployeesController.php I have autocomplete function which is like
function autoComplete() {
echo $this->params['url']['q'] . "---";
$this->loadModel("Department");
$this->set('departments', $this->Department->find('all', array(
'conditions' => array(
'Department.name LIKE ' => '%'.$this->params['url']['q'].'%'
),
'limit' => $this->params['url']['limit'],
'fields' => array('name')
)));
$this->layout = 'ajax';
}
It's not working. What could be the mistake I am doing?
it gives me following error:
Notice (8): Undefined variable: ajax [APP\View\Employees\add.ctp, line 84]
Fatal error: Call to a member function autoComplete() on a non-object in C:\xampplite\htdocs\cakephp\app\View\Employees\add.ctp on line 84
Assuming you have added the helper to the $helpers array of your controller, you have to access the helper with $this->Ajax->autoComplete() instead of $ajax->autoComplete().
Related
This is Cakephp 3.5, So I have three businesses 1, 2, 3. I have a view to show Estimated Revenue From Fees.
On the index page I have a three column Div
<div class="row">
<div class="col-md-12">
<h2><?= __('Estimated Revenue From Fees') ?></h2>
<div class="row">
<div class="column">
<h4>Business One</h4>
<ul>
<?php foreach ($estimatedRevenueFromCFeesBiz1 as $estimatedRevenueFromFeeBiz1): ?>
<li class="actions">
<?= $this->Html->link(__( h($estimatedRevenueFromFeeBiz1->year), ['action' => 'view', $estimatedRevenueFromFeeBiz1->id]) ?>
</li>
<?php endforeach; ?>
</ul>
</div>
<div class="column">
<h4>Business Two</h4>
<?php foreach ($estimatedRevenueFromFeesNauBiz2 as $estimatedRevenueFromFeeBiz2): ?>
<li class="actions">
<?= $this->Html->link(__( h($estimatedRevenueFromFeeNauBiz2->year), ['action' => 'view', $estimatedRevenueFromFeeBiz2->id]) ?>
</li>
<?php endforeach; ?>
</div>
<div class="column">
<h4>Business Three</h4>
<?php foreach ($estimatedRevenueFromCourseFeesUa as $estimatedRevenueFromCourseFeeUa): ?>
<li class="actions">
<?= $this->Html->link(__( h($estimatedRevenueFromFeeBiz3->year), ['action' => 'view', $estimatedRevenueFromFeeBiz3->id]) ?>
</li>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
In my controller I've set up like this:
public function index()
{
$estimatedRevenueFromFeesBiz1 = $this->EstimatedRevenueFromFeesBiz1->find('list', array( 'conditions' => 'business_id' == 1));
$estimatedRevenueFromFeesBiz2 = $this->EstimatedRevenueFromFeesBiz2->find('list', array( 'conditions' => 'business_id' == 2));
$estimatedRevenueFeesBiz3 = $this->EstimatedRevenueFromFeesBiz3->find('list', array( 'conditions' => 'business_id' == 1));
$this->set(compact('estimatedRevenueFromFeesBiz1', 'estimatedRevenueFromFeesBiz2', 'estimatedRevenueFromFeesBiz3'));
}
I'm getting a Call to a member function find() on boolean
Error in: ROOT\src\Controller\EstimatedRevenueFromCourseFeesController.php, line 26 which is the
$estimatedRevenueFromFeesBiz1 = $this->EstimatedRevenueFromFeesBiz1->find('list', array( 'conditions' => 'business_id' == 1)); line.
I figured it out. I changed the controller back to just calling the whole table:
public function index()
{
$this->paginate = [
'contain' => ['Businesses']
];
$estimatedRevenueFromFees = $this->paginate($this->EstimatedRevenueFromFees);
$this->set(compact('estimatedRevenueFromFees'));
}
I then added a condition into the index.ctp file:
<h4>Business One</h4>
<ul>
<?php foreach ($estimatedRevenueFromFees as $estimatedRevenueFromFee): ?>
<?php if ($estimatedRevenueFromFee['business_id'] == 1): ?>
<li class="actions">
<?= $this->Html->link(__( h($estimatedRevenueFromFee->year), ['action' => 'view', $estimatedRevenueFromFee->id]) ?>
</li>
<?php else: ?>
<?php continue; ?>
<?php endif; ?>
<?php endforeach; ?>
</ul>
Well, based on what you said and on your own answer, it looks like what you were trying to do was this:
public function index() {
$estimatedRevenueFromFeesBiz1 = $this->EstimatedRevenueFromFees->find('list', ['conditions' => ['business_id' == 1]]);
$estimatedRevenueFromFeesBiz2 = $this->EstimatedRevenueFromFees->find('list', ['conditions' => ['business_id' == 2]]);
$estimatedRevenueFeesBiz3 = $this->EstimatedRevenueFromFees->find('list', ['conditions' => ['business_id' == 1]]);
$this->set(compact('estimatedRevenueFromFeesBiz1', 'estimatedRevenueFromFeesBiz2', 'estimatedRevenueFromFeesBiz3'));
}
What changes is your condition (business_id). But you are always querying over the same table (EstimatedRevenueFromFees).
Bare in mind that in your solution you are paginating. So you might end up having every record in a page with a certain business_id, or whatever combination.
I have this link that I use to for my modal
<?php
echo $this->Html->link('Create Schedule', '#', array('id' => 'createSchedule', 'class' => 'btn btn-success', 'role' => 'button', ));
?>
How do I convert it to $this->Form->end() format?
Thank you!
You can do this also
<?php
echo $this->Form->button("Create Schedule", array("id" => "createSchedule", "class" => "btn btn-success", "type" => "submit"));
echo $this->Form->end();
?>
or you could also do this
<?php
echo $this->Form->submit("Create Schedule", array("id" => "createSchedule", "class" => "btn btn-success"));
echo $this->Form->end();
?>
You can try this
<?php
$options = array(
'label' => 'Create Schedule',
'id' => 'createSchedule',
'class' => 'btn btn-success'
);
echo $this->Form->end($options);
?>
I am currently trying to solve a problem which I just noticed since the property didn't actually have a main_image allocated to it.
But as soon as I allocated a main image to it. The Related images caused problems.
Property table has a belongsTo relation
public $belongsTo = array( 'Image' => array(
'className' => 'Image',
'foreignKey' => 'main_image_id'
));
and also a hasMany
public $hasMany = array(
'Image' => array(
'className' => 'Image',
'foreignKey' => 'property_id'
)
);
the problem is when I am populating the related images I am receiving multiple errors similar to the below
Illegal string offset 'id'
my data is like so
property(array)
Image(array)
id1
descriptionPainted Brick Design
imageuploads/properties/3/Amazing-Painted-Brick-Houses-Design.jpeg
property_id3
0(array)
id1
descriptionPainted Brick Design
imageuploads/properties/3/Amazing-Painted-Brick-Houses-Design.jpeg
property_id3
and the view was generated like so
<div class="related">
<h3><?php echo __('Related Images'); ?></h3>
<?php if (!empty($property['Image'])): ?>
<table cellpadding = "0" cellspacing = "0">
<tr>
<th><?php echo __('Id'); ?></th>
<th><?php echo __('Description'); ?></th>
<th><?php echo __('Image'); ?></th>
<th><?php echo __('Property Id'); ?></th>
<th class="actions"><?php echo __('Actions'); ?></th>
</tr>
<?php foreach ($property['Image'] as $image): ?>
<tr>
<td><?php echo $image['id']; ?></td>
<td><?php echo $image['description']; ?></td>
<td><?php echo $image['image']; ?></td>
<td><?php echo $image['property_id']; ?></td>
<td class="actions">
<?php echo $this->Html->link(__('View'), array('controller' => 'images', 'action' => 'view', $image['id'])); ?>
<?php echo $this->Html->link(__('Edit'), array('controller' => 'images', 'action' => 'edit', $image['id'])); ?>
<?php echo $this->Form->postLink(__('Delete'), array('controller' => 'images', 'action' => 'delete', $image['id']), null, __('Are you sure you want to delete # %s?', $image['id'])); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
<div class="actions">
<ul>
<li><?php echo $this->Html->link(__('New Image'), array('controller' => 'images', 'action' => 'add')); ?> </li>
</ul>
</div>
</div>
I am not sure if maybe the relations are setup incorrectly or that maybe I would have to include an if to see if there is data before the array and then implement a for loop.
I will answer it rather then comment. You will get all the properties and their main image like this (place this in your controller action eg. index()):
$properties= $this->Property->find('all', array(
'contain' => array(
'Image' => array(
'conditions' => array(
'main_image' => true
)
)
)
));
$this->set('properties', $properties);
and in your view:
<img src='<?php echo '/realestateagencyadministration/img/' . $property['Image'][0]['main_image'] ?>' style='max-width: 100%;height:150px;;'>
Don't take my for sure on this last one, I am wrinting this out of my head, I think you need [0], but you can easily fix that.
I am using cakephp 2.4
in my apps I have a view in CategoriesController namely "Editorial" and I can show all the articles under that category by http://mydomain/categories/editorial
I am trying to show all the articles under "Editorial" category in the home.ctp by echo $this->element('editorials');
but it shows Notice (8): Undefined variable: articles [APP\View\Elements\Editorials.ctp, line 4]
CategoriesController.php
public function Editorial() {
$category = $this->Category->find('first', array(
'conditions' => array(
'Category.id' => 1
)
));
$this->set(compact('category'));
$this->Paginator = $this->Components->load('Paginator');
$this->Paginator->settings = array(
'Article' => array(
'recursive' => -1,
'contain' => array(
'Category'
),
'limit' => 5,
'conditions' => array(
'Article.category_id' => $category['Category']['id'],
),
'order' => array(
'Article.id' => 'ASC'
),
'paramType' => 'querystring',
)
);
$articles = $this->Paginator->paginate($this->Category->Article);
$this->set(compact('articles'));
}
View file:
<?php if (!empty($articles)): ?>
<?php echo $this->element('editorials'); ?>
<?php echo $this->element('pagination-counter'); ?>
<?php echo $this->element('pagination'); ?>
<?php endif; ?>
Elements/Editorials.ctp:
<div class="row">
<?php
$i = 0;
foreach ($articles as $art):
$i++;
if (($i % 4) == 0) { echo "\n<div class=\"row\">\n\n";}
?>
<div class="col col-sm-3">
<?php echo $this->Html->link($art['Article']['title'], array('controller' => 'articles', 'action' => 'view', 'id' => $art['Article']['id'])); ?>
<br />
</div>
<?php
if (($i % 4) == 0) { echo "\n</div>\n\n";}
endforeach;
?>
<br />
<br />
</div>
try to add $this->render('file'); in your Editorial action and replace file with view file name without.ctp
Something you can change:
1. In controller:
$this->set(compact('category', 'articles')); // one set()
2. In view: (Main Point)
<?php echo $this->element('editorials', array('articles' => $articles)); ?>
Here, you need to pass the articles variable to element like above, by default an element don't recognize the viewVars.
I need some help with a little strange problem.
My problem is that submit button does not submit files to controller when using CMultiFileUpload.
I have this very simple view to upload multiple files:
<?php
$form = $this->beginWidget(
'CActiveForm',
array(
'id' => 'upload-form',
'htmlOptions'=>array('enctype' => 'multipart/form-data'),
));?>
<div class="row">
<?php echo $form->labelEx($model,'sourceCode'); ?>
<?php
$this->widget('CMultiFileUpload', array(
'model'=>$model,
'name' => 'sourceCode',
'attribute'=> 'sourceCode',
'max'=>5,
'accept' =>'zip',
'duplicate' => 'Duplicate file!',
'denied' => 'Invalid file type',));
echo $form->error($model,'sourceCode');?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php $this->endWidget();?>
Here is my model:
class UploadSolutionForm extends CFormModel
{
public $sourceCode;
public function rules()
{
return array(
array('sourceCode', 'file', 'types'=>'zip', 'allowEmpty'=>false, 'wrongType'=>'Only .zip files allowed'),
);
}
public function attributeLabels()
{
return array(
'sourceCode' => 'Uploaded file',
);
}
}
And here is probably the simplest action ever:
public function actionUpload()
{
$model = new UploadSolutionForm();
if(isset($_POST['UploadSolutionForm']))
{
echo 'Got it!';
}
$this->render('solve',array('model'=>$model));
}
But when I click on submit it does not echo anything, but it does with this code (CMultiFileUpload is replaced with fileField):
<?php
$form = $this->beginWidget(
'CActiveForm',
array(
'id' => 'upload-form',
'htmlOptions'=>array('enctype' => 'multipart/form-data'),
));?>
<div class="row">
<?php echo $form->labelEx($model, 'sourceCode');?>
<br><?php echo $form->fileField($model, 'sourceCode');?>
<?php echo $form->error($model, 'sourceCode');?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php
$this->endWidget();?>
Can someone tell me what's wrong?
Thanks in advance...
Try this it works
In your View file specify action attribute in form widget as shown in the below code
<?php
$form = $this->beginWidget(
'CActiveForm',
array(
'id' => 'upload-form',
'action'=>Yii::app()->createAbsoluteUrl('yourcontrollername/actionname'),
'htmlOptions'=>array('enctype' => 'multipart/form-data'),
));?>
<div class="row">
<?php $this->widget('CMultiFileUpload',array(
'name'=>'files',
'accept'=>'jpg|png',
'max'=>3,
'remove'=>Yii::t('ui','Remove'),
//'denied'=>'', message that is displayed when a file type is not allowed
//'duplicate'=>'', message that is displayed when a file appears twice
'htmlOptions'=>array('size'=>25),
)); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php $this->endWidget();?>
In yourController actionUplaod as shown below
public function actionUpload(){
echo "hi";
var_dump($_FILES['files']);
}