I am working with laravel5.4. I have used Polymorphic relation for image upload.
So I have created table as images.
image table :
In image model I have created function for relation that looks like below.
Image model :
public function imageInfo()
{
return $this->morphTo();
}
In ticket model I have created function like below
Ticket model :
public function images()
{
return $this->morphMany('App\Models\Image\Image', 'imageInfo');
}
In ticket controller for storing image I have create functionality like :
if($request->file('file') != "")
{
$createPath = public_path('images/upload/ticket');
if(!File::exists($createPath.'/'.$ticket->id))
{
File::makeDirectory($createPath.'/'.$ticket->id,0775,true);
}
$destination_path = public_path('images/upload/ticket/'.$ticket->id);
$files = $request->file('file');
foreach ($files as $file) {
$image = new Image;
$image->name = $file->getClientOriginalName();
$image->type = $file->getClientMimeType();
if($ticket_info->images()->save($image))
{
$file->move($destination_path,$file->getClientOriginalName());
}
}
}
It works perfect for storing image in database. But I want to update this table so according to ticket_id.
So how can I update this table using sync() method or any other option?
Related
So i am changing my project from core PHP to Codeigniter. I have a edit for in which their are 3 input type file. Now i want to run my query according to these inout file. like if input file 1 is empty than don't update value else update. I did it in core PHP like this:
The name of input type file are: image1, image2, image3
mysqli query in core PHP:
if(!empty($photo1))
{
$p1=str_replace("-","",time().$photo1);
move_uploaded_file($_FILES['photo1']['tmp_name'], $folder.$p1);
$query .=",`image1`='$p1'";
}
if(!empty($photo2))
{
$p2=str_replace("-","",time().$photo2);
move_uploaded_file($_FILES['photo2']['tmp_name'], $folder.$p2);
$query .=",`image2`='$p2'";
}
if(!empty($photo3))
{
$p3=str_replace("-","",time().$photo3);
move_uploaded_file($_FILES['photo3']['tmp_name'], $folder.$p3);
$query .=",`image3`='$p3'";
}
Now how to do this in codeigniter model:
controller
public function store_ad()
{
$id=$this->input->post('id');
$d_email=$this->session->userdata('dealer_email');
$p_type=$this->input->post('property_type');
$p_subtype=$this->input->post('property_subtype');
$p_for=$this->input->post('p_for');
$p_name=$this->input->post('p_name');
$p_price=$this->input->post('p_price');
$state=$this->input->post('state');
$city=$this->input->post('city');
$loc=$this->input->post('location');
$pincode=$this->input->post('pincode');
$about=$this->input->post('p_about');
$stat=$this->input->post('status');
$config['upload_path'] = './images/properties';
$config['allowed_types']='gif|png|jpeg|jpg';
$this->load->library('upload',$config);
if($this->upload->do_upload('image1'))
{
$data1=$this->upload->data();
$img1=$data1['raw_name'].$data1['file_ext'];
}
if($this->upload->do_upload('image2'))
{
$data2=$this->upload->data();
$img2=$data2['raw_name'].$data2['file_ext'];
}
if($this->upload->do_upload('image3'))
{
$data3=$this->upload->data();
$img3=$data3['raw_name'].$data3['file_ext'];
}
}
How to crearte model according to the non empty input file. I am storing the name of the file in database
Inside your method in the model class check if the files you want to upload is empty or not e.g
public function update($data_array, $img1, $img2, $img3){
if(!empty($img1)){
$data_array['img_column1'] = $img1;
}
if(!empty($img2)){
$data_array['img_column2'] = $img2;
}
if(!empty($img3)){
$data_array['img_column3'] = $img3;
}
return $this->db->insert('YOUR_TABLE_NAME', $data_array);
}
Hope this helps....
I got a problem to save custom values for embededforms in root form.
I can actually edit a "manifestation" and i can add as much as i want "commande_wifi".
Everything is good saved.
I need to customize the process for every "commande_wifi" ( there is a 'puht' value depending on other values of the object() ). I have already lost a few hours only to do that.
save() is only called on the root form
That’s right! Only the root form has save() called. So if there’s other logic you want to run, you will want to override the saveEmbeddedForm method and call that code before. Oversimplification ahead: when you save a form with embedded forms, it calls $this->getObject()->save(), then it calls saveEmbeddedForms, which, for each embedded form, calls $form->saveEmbeddedForms() and then calls $form->getObject()->save(). This is critical to know, as it will save you a lot of headaches later on.
http://jmather.com/2011/01/29/6-things-to-know-about-embedded-forms-in-symfony/
I've tried to overwrite the saveembededForms() but fail at this point.
class manifestationForm extends BasemanifestationForm
{
public function configure()
{
$this->embedRelation('commande_wifi');
}
public function addNewFields($number){
$new_commandes = new BaseForm();
for($i=0; $i <= $number; $i+=1){
$commande = new Commande_wifi();
$commande->setManifestation($this->getObject());
$commande_form = new commande_wifiForm($commande);
$new_commandes->embedForm($i,$commande_form);
}
$this->embedForm('new', $new_commandes);
}
public function bind(array $taintedValues = null, array $taintedFiles = null){
$new_commandes = new BaseForm();
foreach($taintedValues['new'] as $key => $new_commande){
$commande = new Commande_wifi();
$commande->setManifestation($this->getObject());
$commande_form = new commande_wifiForm($commande);
$new_commandes->embedForm($key,$commande_form);
}
$this->embedForm('new',$new_commandes);
parent::bind($taintedValues, $taintedFiles);
}
public function saveEmbeddedForm($con = null, $forms = null)
{
if ($con === NULL)
{
$con = $this->getConnection();
}
if ($forms === NULL)
{
$forms = $this->getEmbeddedForms();
}
foreach ($forms as $form)
{
if ($form instanceof sfFormObject)
{
$form->saveEmbeddedForms($con);
$form->getObject()->setPuht(99);
$form->getObject()->save($con);
}
else
{
$this->saveEmbeddedForms($con, $form->getEmbeddedForms());
}
//$form->getObject()->setPuht(99)->save();
}
}
}
It's won ASAP i can access the embedForm Object().
Any suggestion?
I want to create a pdf of first 300 orders in magento. I want a functionality in which i will get first 300 orders and print their images(each order has different image) in a pdf. So how can i implement this functionality in magento. Is there any extension for that?
Take a look at /app/code/core/Mage/Adminhtml/controllers/Sales/OrderController.php
public function pdfinvoicesAction(){
$orderIds = $this->getRequest()->getPost('order_ids');
$flag = false;
if (!empty($orderIds)) {
foreach ($orderIds as $orderId) {
$invoices = Mage::getResourceModel('sales/order_invoice_collection')
->setOrderFilter($orderId)
->load();
if ($invoices->getSize() > 0) {
$flag = true;
if (!isset($pdf)){
$pdf = Mage::getModel('sales/order_pdf_invoice')->getPdf($invoices);
} else {
$pages = Mage::getModel('sales/order_pdf_invoice')->getPdf($invoices);
$pdf->pages = array_merge ($pdf->pages, $pages->pages);
}
}
}
if ($flag) {
return $this->_prepareDownloadResponse(
'invoice'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').'.pdf', $pdf->render(),
'application/pdf'
);
} else {
$this->_getSession()->addError($this->__('There are no printable documents related to selected orders.'));
$this->_redirect('*/*/');
}
}
$this->_redirect('*/*/');
}
From the above function you could assign the first 300 order ids to $orderIds (or modify Mage::getResourceModel('sales/order_invoice_collection to get the first 300 records)
See magento orders list query
Changes :
public function pdfinvoicesAction(){
$orderIds = $this->getRequest()->getPost('order_ids');
To (something like)
public function pdfinvoices($orderIds){
$orderIds = (array) $orderIds; // first 300 record ids
Change line to save pdf to file
return $this->_prepareDownloadResponse(
'invoice'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').'.pdf', $pdf->render(),
'application/pdf'
);
To
$pdf->render();
// use the order_id for the pdf name like
$pdf->save("{$orderId}.pdf");
see Error in generated pdf file using zend_pdf under Magento
You could also delete the $this->_redirect('//')
I have a many to many relation between Product and Properties. I'm using embedRelation() in my Product form to edit a Product and it's Properties. Properties includes images which causes my issue. Every time I save the form the updated_at column is updated for file properties even when no file is uploaded.
Therefore, I want to exclude empty properties when saving my form.
I'm using Symfony 1.4 and Doctrine 1.2.
I'm thinking something like this in my ProductForm.class.php, but I need some input on how to make this work.
Thanks
class ProductForm extends BaseProductForm
{
public function configure()
{
unset($this['created_at'], $this['updated_at'], $this['id'], $this['slug']);
$this->embedRelation('ProductProperties');
}
public function saveEmbeddedForms($con = null, $forms = null)
{
if (null === $forms)
{
$properties = $this->getValue('ProductProperties');
$forms = $this->embeddedForms;
foreach($properties as $p)
{
// If property value is empty, unset from $forms['ProductProperties']
}
}
}
}
I ended up avoiding Symfony's forms and saving models instead of saving forms. It can be easier when playing with embedded forms. http://arialdomartini.wordpress.com/2011/04/01/how-to-kill-symfony%E2%80%99s-forms-and-live-well/
Solved it by checking if posted value is a file, and if both filename and value_delete is null I unset from the array. It might not be best practice, but it works for now.
Solution based on http://www.symfony-project.org/more-with-symfony/1_4/en/06-Advanced-Forms
class ProductPropertyValidatorSchema extends sfValidatorSchema
{
protected function configure($options = array(), $messages = array())
{
// N0thing to configure
}
protected function doClean($values)
{
$errorSchema = new sfValidatorErrorSchema($this);
foreach($values as $key => $value)
{
$errorSchemaLocal = new sfValidatorErrorSchema($this);
if(array_key_exists('value_delete', $values))
{
if(!$value && !$values['value_delete'])
{
unset($values[$key]);
}
}
// Some error for this embedded-form
if (count($errorSchemaLocal))
{
$errorSchema->addError($errorSchemaLocal, (string) $key);
}
}
// Throws the error for the main form
if (count($errorSchema))
{
throw new sfValidatorErrorSchema($this, $errorSchema);
}
return $values;
}
}
I'm learning Zend Framework. I had Zend_Paginator working with the array adapter, but I'm having trouble using the Zend_Paginator::factory static method. The problem is the pagination control links send me to the correct URL, but the results disappear when I click next or page 2, 3, etc.
I have two tables from a database: a file table and an origination_office table. The file table has the client's names, address, etc. and the origination office stores office names (like Tampa, Sarasota, etc.). Each file is associated with an origination office.
My controller:
public function searchAction()
{
$searchForm = new Application_Form_SearchForm();
if ($this->_request->getQuery()) {
if ($searchForm->isValid($this->_request->getParams())) {
$officeName = $this->_request->getParam('officeName');
$page = $this->_request->getParam('page');
}
$fileTable = new Application_Model_DbTable_File();
$this->view->paginator = $fileTable->getFilesByOfficeName($officeName, $page);
}
$this->view->searchForm = $searchForm;
}
Here is the getFilesByOfficeName() method:
public function getFilesByOfficeName($officeName = null, $page = 1, $count = 12, $range = 15, $scrolling = 'Sliding')
{
if (is_null($officeName)) {
$query = $this->select();
$paginator = Zend_Paginator::factory($query);
} else {
$oofTable = new Application_Model_DbTable_OriginationOffice();
$query = $oofTable->select();
$query->where('oof_name like ?', $officeName.'%');
if ($oofTable->fetchRow($query)) {
$origination_office = $oofTable->fetchRow($query);
$files = $origination_office->findDependentRowset($this);
$paginator = Zend_Paginator::factory($files);
} else {
return;
}
}
Zend_Paginator::setDefaultScrollingStyle($scrolling);
Zend_View_Helper_PaginationControl::setDefaultViewPartial('_pagination_control.phtml');
$paginator->setDefaultItemCountPerPage($count);
$paginator->setDefaultPageRange($range);
$paginator->setCurrentPageNumber($page);
return $paginator;
}
Ok, I think I am understanding your problem. Your links are not maintaining the state of your initial request and it's URL query string.
You might want to edit your partial view (_pagination_control.phtml) to render the current query string in your links.
I would need to see what your doing in the partial to give an exact answer, but this should work if you add ?$_SERVER['QUERY_STRING'] to the end of your final URL. See Below Example:
<!-- Your href may look different but notice I append the query string to the end -->
Last »