This document was uploaded by user and they confirmed that they have the permission to share
it. If you are author or own the copyright of this book, please report to us by using this DMCA
report form. Report DMCA
Overview
Download & View Curso Symfony Clase 2 as PDF for free.
class jobActions extends sfActions { public function executeIndex(sfWebRequest $request) { $this‐>listado = JobeetJobPeer::doSelect( new Criteria() ); } }
public function executeIndex(sfWebRequest $request) { $criteria = new Criteria(); $criteria‐>add( JobeetJobPeer::CREATED_AT EXPIRES_AT, time(), Criteria::GREATER_THAN ); $this‐>listado = JobeetJobPeer::doSelect( $criteria ); }
Personalizando la configuración
¿Dónde está el problema? public function executeIndex(sfWebRequest $request) { $criteria = new Criteria(); $criteria‐>add( JobeetJobPeer::CREATED_AT, mejor como opción time() ‐ 86400 * 30, de configuración Criteria::GREATER_THAN ); $this‐>listado =
¿Dónde está el problema? Controlador (MVC) public function executeIndex(sfWebRequest $request) { $criteria = new Criteria(); $criteria‐>add( JobeetJobPeer::CREATED_AT, Modelo (MVC) time() ‐ 86400 * 30, Criteria::GREATER_THAN ); $this‐>listado =
}
JobeetJobPeer::doSelect($criteria);
Modelo
lib/model/JobeetJobPeer.php
class JobeetJobPeer extends BaseJobeetJobPeer { static public function getActiveJobs() { $criteria = new Criteria(); $criteria‐>add( self::EXPIRES_AT, time(), Criteria::GREATER_THAN ); return self::doSelect($criteria); } }
Controlador
apps/frontend/modules/job/actions/
actions.class.php
public function executeIndex(sfWebRequest $request) { $this‐>jobeet_job_list = JobeetJobPeer::getActiveJobs(); }
static public function getActiveJobs() { $criteria = new Criteria(); $criteria‐>add( self::EXPIRES_AT, time(), Criteria::GREATER_THAN );
$criteria‐>addDescendingOrderByColumn( self::EXPIRES_AT ORDER BY ___ DESC ); return self::doSelect($criteria); }
Mostrando las categorías en la portada
lib/model/JobeetCategoryPeer.php class JobeetCategoryPeer extends BaseJobeetCategoryPeer { static public function getWithJobs() { $criteria = new Criteria(); $criteria‐>addJoin(self::ID, JobeetJobPeer::CATEGORY_ID); $criteria‐>add( JobeetJobPeer::EXPIRES_AT, time(), Criteria::GREATER_THAN );
$criteria‐>setDistinct();
SELECT DISTINCT
return self::doSelect($criteria); } }
apps/frontend/modules/job/actions/actions.class.php public function executeIndex(sfWebRequest $request) { $this‐>categories = JobeetCategoryPeer::getWithJobs(); }
lib/model/JobeetJobPeer.php class JobeetJobPeer extends BaseJobeetJobPeer { static public function getActiveJobs($max = 10) { $criteria = new Criteria(); $criteria‐>add( mejor en un archivo self::EXPIRES_AT, de configuración time(), Criteria::GREATER_THAN ); $criteria‐>setLimit($max); return self::doSelect($criteria); } }
DRY (Don’t Repeat Yourself) A process philosophy aimed at reducing duplication, particularly in computing. […] When the DRY principle is applied successfully, a modification of any single element of a system does not change other logically‐ unrelated elements. Additionally, elements that are logically related all change predictably and uniformly, and are thus kept in sync.
test/unit/JobeetTest.php require_once dirname(__FILE__).'/../bootstrap/unit.php'; $t = new lime_test(1, new lime_output_color()); $t‐>pass('This test always passes.');
test/unit/JobeetTest.php require_once dirname(__FILE__).'/../bootstrap/unit.php'; $t = new lime_test(6, new lime_output_color()); $t‐>comment('::slugify()'); $t‐>is(Jobeet::slugify('Sensio'), 'sensio', '::slugify() pasa la cadena de texto a minúsculas'); $t‐>is(Jobeet::slugify('sensio labs'), 'sensio‐labs', '::slugify() sustituye los espacios en blanco por ‐'); ...
'frontend', 'test', true ); new sfDatabaseManager($configuration); $loader = new sfPropelData(); $loader‐>loadData(sfConfig::get('sf_test_dir').'/fixtures');
test/unit/model/JobeetJobTest.php include(dirname(__FILE__).'/../../bootstrap/propel.php'); $t = new lime_test(1, new lime_output_color()); $t‐>comment('‐>getCompanySlug()'); $job = JobeetJobPeer::doSelectOne(new Criteria()); $t‐>is( $job‐>getCompanySlug(), Jobeet::slugify($job‐>getCompany()), '‐>getCompanySlug() devuelve el slug del nombre de la empresa' );
Symfony ya incluye... • Validación (para cada campo) • Widgets (campos del formulario) • Formularios (widgets + validación)
Formularios
class ContactForm extends sfForm { public function configure() { $this‐>setWidgets(array( 'email' => new sfWidgetFormInput(), 'message' => new sfWidgetFormTextarea(), )); $this‐>setValidators(array( 'email' => new sfValidatorEmail(), 'message' => new sfValidatorString(array( 'max_length' => 255) ), )); } }
sfWidgetFormChoice
sfWidgetFormInputHidden
sfWidgetFormChoiceMany
sfWidgetFormInputPassword
sfWidgetFormDate
sfWidgetFormPropelChoice
sfWidgetFormDateRange
sfWidgetFormPropelChoiceMany
sfWidgetFormDateTime
sfWidgetFormPropelSelect
sfWidgetFormFilterDate
sfWidgetFormPropelSelectMany
sfWidgetFormFilterInput
sfWidgetFormSchema
sfWidgetFormI18nDate
sfWidgetFormSchemaDecorator
sfWidgetFormI18nDateTime
sfWidgetFormSchemaForEach
sfWidgetFormI18nSelectCountry
sfWidgetFormSchemaFormatter
sfWidgetFormI18nSelectCurrency
sfWidgetFormSelect
sfWidgetFormI18nSelectLanguage
sfWidgetFormSelectCheckbox
sfWidgetFormI18nTime
sfWidgetFormSelectMany
sfWidgetFormInput
sfWidgetFormSelectRadio
sfWidgetFormInputCheckbox
sfWidgetFormTextarea
sfWidgetFormInputFile
sfWidgetFormTime
sfWidgetFormInputFileEditable
$this‐>mergeForm(new OtroForm()); $this‐>embedForm('name', new OtroForm());
Formularios de Propel
schema.yml $ ./symfony propel:build‐forms
JobeetJobForm
JobeetCategoryForm
lib/form/
JobeetAffiliateForm
class JobeetJobForm extends BaseJobeetJobForm { public function configure() { unset( $this['created_at'], $this['updated_at'], $this['expires_at'], $this['is_activated'] ); } }
class JobeetJobForm extends BaseJobeetJobForm { public function configure() { ... $this‐>validatorSchema['email'] = new sfValidatorEmail(); } }
class JobeetJobForm extends BaseJobeetJobForm { public function configure() { ... $this‐>widgetSchema['type'] = new sfWidgetFormChoice(array( 'choices' => JobeetJobPeer::$types, 'expanded' => true, )); } class JobeetJobPeer extends BaseJobeetJobPeer { static public $types = array( } 'full‐time' => 'Full time', 'part‐time' => 'Part time', 'freelance' => 'Freelance', ); // ... }
sfWidgetFormChoice multiple
expanded
false
false
true
false
false
true
true
true
widget
class JobeetJobForm extends BaseJobeetJobForm { public function configure() { ... $this‐>validatorSchema['type'] = new sfValidatorChoice(array( 'choices' => array_keys(JobeetJobPeer::$types), ));
} }
class JobeetJobForm extends BaseJobeetJobForm { public function configure() { ... $this‐>widgetSchema‐>setLabels(array( 'category_id' => 'Category', 'is_public' => 'Public?', 'how_to_apply' => 'How to apply?', )); } }
class JobeetJobForm extends BaseJobeetJobForm { public function configure() { ... $this‐>widgetSchema['logo'] = new sfWidgetFormInputFile(array( 'label' => 'Company logo', )); $this‐>validatorSchema['logo'] = new sfValidatorFile(array( 'required' => false, 'label' => sfConfig::get('sf_upload_dir').'/jobs', 'mime_types' => 'web_images', )); } }
sfValidatorFile 1.
Valida que el archivo subido sea una imagen
2.
Cambia el nombre del archivo por un valor único
3.
Guarda el archivo en la ruta indicada
4.
Actualiza el valor de la columna logo
class JobeetJobForm extends BaseJobeetJobForm { public function configure() { ... $this‐>widgetSchema‐>setHelp( 'is_public', 'Indica si la oferta de trabajo se puede publicar en sitios web de afiliados' ); } }
apps/frontend/modules/job/templates/
newSuccess.php
Post a Job
$form)) ?>
parcial _form
apps/frontend/modules/job/templates/
_form.php
method, enctype
Formulario
Widgets
render()
renderRow()
renderHiddenFields()
render()
hasErrors()
renderLabel()
hasGlobalErrors()
renderError()
getGlobalErrors()
renderHelp()
renderGlobalErrors()
renderRow() ?>
apps/frontend/modules/job/actions/ public function
executeNew(sfWebRequest
actions.class.php
$request) {
... } public function
executeCreate(sfWebRequest
$request) {
... } public function
executeEdit(sfWebRequest
$request) {
... } public function
executeUpdate(sfWebRequest
$request) {
executeDelete(sfWebRequest
$request) {
... } public function
... } protected function
{ ... }
processForm(sfWebRequest
$request, sfForm $form)
apps/frontend/modules/job/actions/
actions.class.php
public function executeNew(sfWebRequest $request) { $job = new JobeetJob(); $job‐>setType('full‐time'); $this‐>form = new JobeetJobForm($job); }
lib/model/
JobeetJob.php
public function save(PropelPDO $con = null) { // ... if (!$this‐>getToken()) { $this‐>setToken( sha1($this‐>getEmail().rand(11111, 99999)) ); } return parent::save($con); } lib/form/
JobeetJobForm.class.php
class JobeetJobForm extends BaseJobeetJobForm { public function configure() { unset( $this['token'] ); } }