I'm trying to add some extra logic to update and delete actions in a module generated by task doctrine:generate-admin. I read here and see that I can create executeUpdate() and executeDelete() methods in /application/mymodule/actions/actions.class.php. Because executeUpdate() call proccessForm() method then I think the best is to override this instead of executeUpdate() but don't know how. The code I need to add is this:
$q = Doctrine_Core::getTable('SdrivingEmpresa')
->createQuery('a')
->where('idempresa = ?', $request->getParameter('idempresa'))
->execute();
$file = new sfFilesystem();
$file->remove(sfConfig::get('sf_upload_dir') . '/' . $q[0]->getLogotipo());
I do it for executeDelete() by copying the generated cache coded and writing my logic see below:
public function executeDelete(sfWebRequest $request) {
$request->checkCSRFProtection();
$this->dispatcher->notify(new sfEvent($this, 'admin.delete_object', array('object' => $this->getRoute()->getObject())));
$q = Doctrine_Core::getTable('SdrivingEmpresa')
->createQuery('a')
->where('idempresa = ?', $request->getParameter('idempresa'))
->execute();
$file = new sfFilesystem();
$file->remove(sfConfig::get('sf_upload_dir') . '/' . $q[0]->getLogotipo());
if ($this->getRoute()->getObject()->delete()) {
$this->getUser()->setFlash('notice', 'The item was deleted successfully.');
}
$this->redirect('#sdriving_empresa');
}
But I tough this is not right, so my question is, should I do the same with processForm() or exists a better way to do this? any advice or help?
You can copy the proccessForm() generated in the cache of the application in your admin module action to override it.
Related
I am using Laravel 5. In my Controller I am getting value from View like this method:
$params = array('' => Input::get("selected_category_id"));
then it showing in my log like this:
/api/zones/?=3
I try to modify which remove the single quote and equal symbol, like this:
$params = array(Input::get("selected_category_id"));
then it showing in my log like this:
/api/zones/?0=3
Can I set my url to be like this format:
/api/zones/3
thanks for any help!
As far as I understand your questions, you need to create a new route for it:
Route::post('/api/zones/{id}','YourController#processInput');
In the above route, replace YourController with the controller you are using to process the input and replace processInput with the method that actually processes it.
So, your controller would now have access to the category:
public function processInput(Request $request, $id)
{
$categoryID = $id;
}
I am still very new to Zend and running into some issues on exporting my data to a CSV.
I found a great resource that explains the headers and download part here however I am running into issues when trying to export the actual data.
If I create a variable like $content = "test" the export works fine using the code above.
However when I duplicate my indexAction code, make some changes, and bring it into my downloadAction, I am getting issues that I believe are due to my content being returned as an Object rather than an array or string.
My Module is grabbing the SQL by using:
public function fetchAllMembers($order = null , $order_by = null, $selectwhere = null) {
$session = new SessionContainer('logggedin_user');
$sql = new Sql($this->adapter);
$select = new Select();
$select->from(array('u' => 'tbl_all_data'));
if ($selectwhere != null){
$select->where($selectwhere);
}
$select->order($order_by . ' ' . $order);
$selectString = $sql->getSqlStringForSqlObject($select);
$results = $this->adapter->query($selectString, Adapter::QUERY_MODE_EXECUTE);
$results->buffer();
return $results;
}
and my Controller is calling that SQL by using:
$content = $modulesTable->fetchAllMembers($order, $order_by, $where);
Any help would be greatly appreciated, and I don't need anyone to write the code for me just help with pointoing me in the right direction.
$this->adapter->query returns a Zend\Db\ResultSet object. So you need to call $results = $results->toArray(); to send an array.
Also you need to loop through the array and echo it out in your view file.
Results, returned by adapter are ResultSet type. I guess you need to call at least
current()
method to grab some data. And they will be of array type, so, again you need to do something with them.
toArray() is often used to quickly get data.
More sophisticated way to get data, is to use next() method with current():
$firstThing = $result->current();
$result->next();
$result->next();
$thirdThing = $result->current();
It's just an example, but it can be useful in some cases.
I'm trying to upload file but get this error message :
move_uploaded_file() [<a href='function.move-uploaded-file'>function.move-uploaded-file</a>]: The second argument to copy() function cannot be a directory
I think there's something problem with this file but I've no idea to solve it..
<?php
class FileUploadController extends CController {
public function actionUpload() {
$model = new FileUpload();
$form = new CForm('application.views.fileUpload.uploadForm', $model);
if ($form->submitted('submit') && $form->validate()) {
$form->model->image = CUploadedFile::getInstance($form->model, 'image');
if($model->validate())
{
$model->image->saveAs('/opt/lampp/htdocs/upl/images');
Yii::app()->user->setFlash('success', 'File Uploaded');
$this->redirect(array('upload'));
}
}
$this->render('upload', array('form' => $form));
}
}
?>
You either need to check all files existed at '/opt/lampp/htdocs/upl/images' and check if the same named file is available or not, if available then just rename the file with extra "_1" every time, or you can always upload the file by renaming the file into some machine name sort of thing see the code below,
$name = rand(1000,9999) . time(); // rand(1000,9999) optional
$name = md5($name); //optional
$model->image->saveAs('/opt/lampp/htdocs/upl/images/' . $name . '.jpg');
This is what I usually do with file uploads, provided that you're saving the files references into the database or in any text file.
EDIT
Get Extension.
In case if you're required to get extension of the file rather then of hard-coded, you can use $model->image->getExtensionName(); it will get you the extension of the uploaded file without . (dot)
Finally, I solved it by myself:
The problem was located in line
$model->image->saveAs('/opt/lampp/htdocs/upl/images');
It should be :
$model->image->saveAs('/opt/lampp/htdocs/upl/images/images.jpg');
Now, there's another problem: when I upload 'new image', it will be replace the old file, I want the file(s) being uploaded not replaced the old file or I need something like rename as new file. Does anyone knows?
goto cuploadedfile.php .
over write this function with this
if($this->_error==UPLOAD_ERR_OK)
{
if($deleteTempFile)
return move_uploaded_file($this->_tempName,$file."/".$this->getName());
elseif(is_uploaded_file($this->_tempName))
return copy($this->_tempName, $file."/".$this->getName());
else
return false;
}
thank u.........
Is there a way to get the current view being rendered inside zend framework 2?
I believe this should be possible with the event system but I can't seem to make it work.
The reason I want to get this information is so I can automatically include a .js file with the same name, this would save me time having to specify this rule each time i'm inside a action.
Many thanks,
Tom
I'm not quite sure what you mean by rendering the current view inside ZF2, but here's how you can add a js file named after the action automatically. Just put this in your controller:
public function onDispatch(MvcEvent $mvcEvent)
{
$renderer = $this->serviceLocator->get('Zend\View\Renderer\PhpRenderer');
$actionName = $mvcEvent->getRouteMatch()->getParam('action');
$jsFile = $actionName . '.js';
$baseUrl = $mvcEvent->getRouter()->getBaseUrl();
$renderer->headScript()->appendFile($baseUrl . '/js/' . $jsFile);
return parent::onDispatch($mvcEvent);
}
You may need to adjust the code for your js file location and name of course. The onDispatch method is called automatically before the action.
Thank You Wunibald,
Your example worked perfectly, I have modified it below to be attached to an event so that it applies to every controller/module. To do this I have included it into the onBootstrap function in my Application module.
$events = StaticEventManager::getInstance();
$events->attach('Zend\\Mvc\\Application', 'dispatch', function(\Zend\EventManager\Event $event)
{
$baseUrl = $event->getRouter()->getBaseUrl();
$renderer = $event->getApplication()->getServiceManager()->get('Zend\View\Renderer\PhpRenderer');
$action = $event->getRouteMatch()->getParam('action');
$controller = $event->getRouteMatch()->getParam('controller');
if (strlen($controller) > 0)
{
list($module, $_null, $controller) = explode('\\', $controller);
$renderer->headScript()->appendFile($baseUrl . '/module/' . $module . '/view/' . strtolower($module) . '/' . strtolower($controller) . '/' . strtolower($action) . '.js');
$renderer->headScript()->appendFile($baseUrl . '/module/' . $module . '/view/' . strtolower($module) . '/' . strtolower($module) . '.js');
}
});
Once again thank you for pointing me in the right direction.
I have 3 models:
article [name, title]
photo [name, description]
video [name, description, transcription]
Now, I want to create an autocomplete search that will search each of those models and db fields for matching string in the input field.
Is it possible to do a search over these 3 tables?
If so, how would I go about it?
UNION operator is what you're looking for.
I guess your using propel.
Its absolutetly possible to do this, but if these table are not related to anything that could result in some messy code when saving. But if you absolutely need to search in those tables, my approach would be to create an action like:
public function executeAutocomplete(sfWebRequest $request)
{
$q = $request->getParameter('q');//The value to be searched
$limit = $request->getParameter('limit');//Not completly necessary but recommendable
if(count($q) == 0)
{
return sfView::NONE;//With no input, no search
}
$results = array( 'article' => array(), 'photo' => array(), 'video' => array());
$article_search = ArticlePeer::search($q,$limit);
$photo_search = PhotoPeer::search($q,$limit);
$video_search = VideoPeer::search($q,$limit);
$this->process_search($results,$article_search,$photo_search,$video_search);
//Process those three arrays to fit your need
return $this->renderText(json_encode($result));
}
Now, the search function on the Peer classes could look like this:
ArticlePeer>>
public static function search($q,$limit = null)
{
$c = new Criteria();
$c->add(self::NAME,'%'.$q.'%',Criteria::LIKE);
if(!is_null($limit))
{
$c->addLimit($limit);
}
return self::doSelect($c);
}
Finally as for the widget, i have used and adapted sfWidgetJQueryAutocomplete and it work pretty good so you should check it out.
EDIT: The short way to an embbed search field si creating sfForm wit the jQuery widget i mentioned before and leave configuration and js to the widget. You'll have to find a way to handle search resutls.
Well i hope this helped you!