Reputation: 81
im new to symfony framework i make a jquery ajax form here it runs well it also saves the images to the uploads folder my problem here is it will not save to database the filename. how do i passed the id im not using the symfony form. Here is my code in indexSuccess.php
<script>
$(document).ready(function(){
$("#loading").hide();
$('#send_form').ajaxForm({
target: '.result',
beforeSubmit: validate,
success: function(data) {
$(".result").html(data);
$(".result").show();
}
});
});
function validate(){
var photoValue = $('input[name=files]').fieldValue();
if (!photoValue[0]){
alert('Please upload a picture of you.');
return false;
}
}
</script>
<div id="sf_admin_content">
<h2>Company Gallery</h2>
<br />
<form id="send_form" action="<?php echo url_for('companyGallery/add'); ?>" method="post">
<table id="tbl" class="company" cellspacing="0" style="width:500px;">
<tr>
<td><input type="file" name="files" /></td>
</tr>
<tr>
<td><input type="submit" value="Upload" class="btn btn-success" /></td>
</tr>
</table>
</form>
<br />
<div id="loading" >
<img alt="loading" src="/images/preloader_transparent.gif" />
</div>
<div class="result">
<table id="tbl" class="company" cellspacing="0" style="width:250px;">
<thead>
<tr>
<th>Id</th>
<th>Photo</th>
</tr>
</thead>
<tbody>
<?php foreach($companyGallery as $x => $gallery): ?>
<tr id="company<?php echo $gallery->getId(); ?>" class="<?php echo ($x&1) ? 'even':'odd'; ?>">
<td align="right"><?php echo $gallery->getId(); ?></td>
<td><a href="" target="_blank"><img alt="" src="/uploads/companyGallery/<?php echo $gallery->getImageFull(); ?>" /></a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
in the symfony code below
<?php
class addAction extends sfAction{
private function uploadPhoto($id,$file){
//echo "test";
if(!$id){
$dir = sfConfig::get('app_tempCompGallery_img_dir');
}else{
$dir = sfConfig::get('app_companyGallery_dir');
}
$full = sfConfig::get('app_companyGallery_full');
$small = sfConfig::get('app_companyGallery_small');
$dir = sfConfig::get('app_companyGallery_dir');
$dir_150 = sfConfig::get('app_companyGallery_dir_150');
$file['ext'] = getFileExtension($file['name']);
if(!$id){
$fileName = uniqid(). '.'.$file['ext'];
}else{
$fileName = $id.'.'.$file['ext'];
}
$imagePath = $dir.$fileName;
$image_full = encode('CompanyGallery-'.$id).'.'.$fileName;
$image_small = encode('CompanyGallery-'.$id).'.'.$fileName;
$image_full_path = $dir.$image_full;
$image_small_path = $dir_150.$image_small;
rename($file['tmp_name'],$image_full_path);
$image = new myImageManipulator($image_full_path);
$image->resample2($full, $full);
$image->crop(0, 0, $full, $full);
$image->save($image_full_path);
$image = new myImageManipulator($image_full_path);
$image->resample2($small, $small);
$image->crop(0, 0, $small, $small);
$image->save($image_small_path);
//$companyGallery = $this->getUser()->getAttribute('companyGallery');
//$this->saveData($companyGallery);
echo "test345";
$gallery = Doctrine_Core::getTable('CompanyGallery')->find($id);
//var_dump($gallery);exit();
$gallery->setImageFull($image_full);
$gallery->setImageSmall($image_small);
$gallery->save();
}
public function execute($request){
//echo "test macky 123";
//exit();
$gallery = new CompanyGallery();
print_r($gallery);
$gallery_id = $gallery->getId();
echo "ASDFAS" .$gallery->getId(); exit();
$file = $this->request->getFiles();
print_r($file);
$file = $file['files'];
if($file && $file['error'] == 0){
$this->uploadPhoto($gallery_id,$file);
}
}
}
as youve notice in the $gallery = Doctrine_Core::getTable('CompanyGallery')->find($id);
how do i pass the id in this code? please help..so that it will save to the database the filename
heres my companyTable.class.php in lib/model folder
class CompanyGalleryTable extends Doctrine_Table
{
/**
* Returns an instance of this class.
*
* @return object CompanyGalleryTable
*/
public static function getInstance()
{
return Doctrine_Core::getTable('CompanyGallery');
}
public function getGalleryList($param=array()){
$q = Doctrine_Query::create()
->from('CompanyGallery c')
->orderBy('c.updated_at DESC');
$pager = new sfDoctrinePager('CompanyGallery',10);
$pager->setQuery($q);
$pager->setPage($param['curPage']);
$pager->init();
return $pager;
}
}
Upvotes: 0
Views: 350
Reputation: 342
Why do you need the Id for insert.
You should just create the object of CompanyGallery class and it will save the the record into the database.
$gallery = new CompanyGallery();
Upvotes: 1