Jun 08 2009

MeioUpload project created at github

Category: CakePHPvbmendes @ 10:02

Juan Basso created a project hosted at github. I am unable to keep working on this behavior an he is making some updates to the code. I think github is a great tool because you can simply fork the project and make your own changes. Go check his work: http://github.com/jrbasso/MeioUpload/tree/master

Tags: , , , ,


Jan 28 2009

MeioUpload Behavior documentation in other languages

Category: CakePHPvbmendes @ 20:12

I found out that the documentation of the MeioUpload was translated to other languages and I want to announce it. Here are the links:

Tags: , , , ,


Oct 12 2008

MeioUpload Behavior 1.0.1 released!

Category: CakePHP,PHPvbmendes @ 19:40

I am glad to show you the MeioUpload Behavior 1.0.1. An improved Upload Behavior for CakePHP 1.2. It makes file uploads as simple as defining a variable.

See the docs.

Features

  • Accepts custom directory for files to be uploaded;
  • Validates the file extension and mime-type due to the behavior configuration;
  • Validates the max file size;
  • Allow custom validation rules;
  • Allow as many thumbnails formats as you want;
  • Allow more then one field to be uploadable, with custom options per field;
  • Stores the directory, filesize, and mime-type in the database if the table has these fields;
  • Allow the use of default files and deleting files without deleting the entire record;
  • Delete files when the record is deleted or updated with a new file;
  • Also works in the $model->saveAll method.

Changelog

v1.0.1

  • Fixed a bug in the create folder method;
  • Now you can use the $validate var of the model to apply the changes to default validation rules;
  • Changed the my_array_merge function, now it’s part of the behavior, name arrayMerge;
  • Allow use of {DS}, {model} and {field} constants in directory name and fields names;
  • Fixed a bug with the replacement of the default names.

v1.0

  • Initial release

Any bugs, features suggestions, and english errors, please tell me.

Tags: , ,


Sep 30 2008

Setar flag no lugar de excluir registro do banco com CakePHP

Category: CakePHP,PHPvbmendes @ 19:59

Uma boa prática de programação é setar uma flag indicando que um registro foi excluido, e não simplesmente excluir o registro do banco. Por exemplo, se eu tenho um produto, que está relacionado em vários pedidos, e eu simplesmente excluo o produto, todos os pedidos que referenciam ele serão inconsistentes. Se em troca de excluir o registro do produto, eu simplesmente setar uma flag no registro indicando que ele foi excluído, eu não perderei mais essa consistência. Para excluir o produto de id 5, seria algo como:

mysql_query("UPDATE produtos SET deleted = true WHERE id = 5");

E para listar os produtos, poderia ser algo como:

mysql_query("SELECT * FROM produtos WHERE NOT deleted;");

Percebam que para transferir isso para o CakePHP, basta alterar dois métodos, o index, adicionando a condição, e o delete, substituindo o $this->Produto->del() por um save. Mas o Mariano Iglesias do Cake Syrup desenvolveu um behavior que nos economiza muito trabalho. É o SoftDeletable. Com ele, a tarefa se resume a uma simples configuração no model no qual se deseja aplicar o uso da flag.

var $actsAs = array('SoftDeletable');

Para isso, basta criar o campo deleted na sua tabela e adicionar o arquivo do behavior, disponível no site citado, na pasta de behaviors do seu projeto.

O único problema que eu encontrei nesse behavior é que ao chamar a função $this->Model->del($id), o beforeDelete do behavior é acionado, atualizando o campo da flag deleted para true e retornando false, o que impede que o registro seja realmente excluído do banco. Com isso a tentativa de salvar retorna falso, indicando erroneamente que não obteve sucesso.

Tags: ,