Tuesday, October 30, 2012

Zend Framework 1 and Zend Framework 2

Zend, a PHP company, released Zend Framework 2 stable version recently and I started working on some projects using Zend Framework 2 right after it came out.
I've been working on Magento(an E-Commerce platform that is built on top of Zend Framework 1) for last 4 years.
At the time I started working on Magento, I knew what Zend Framework was but never had any experience with it.
I had more experience with CodeIgniter and I thought CodeIgniter would be a better platform for its simplicity.
However, after I worked on Magento for several months, I became a big fan of Magento and Zend Framework.
Of course, I've been struggling for first couple weeks because of its complexity - I wasn't familiar with Singleton pattern and EAV model.
Now I have more favor on Zend Framework over CodeIgniter.
I would say Zend Framework is complex and difficult to learn than any other PHP frameworks out there, but I think it's the best framework on the market right now.

Features and concepts overview on ZF1 and ZF2:

Main features on Zend Framework 1:

  • MVC(Model View Controller) architecture

  • Singleton

  • Registries

  • Hard & Soft coded Dependencies

  • Loosely coupled architecture



 

Zend Framework 2 resembles Zend Framework 1 in many ways, but it works differently.

Main features on Zend Framework 2:

  • Event Driven Services - Event Manager & Service Manager

  • Dependency Injection - to avoid dependencies, ZF2 uses events

  • Aspect Oriented Programming - shape the workflow using event manager

  • Namespaces - requires PHP 5.3

  • Lambda functions

  • developers can install only the components desired - don't need to install entire framework



 

Useful resources on ZF2:

Wednesday, October 17, 2012

Richard Dawkins at Macky Auditorium

I was at Macky Auditorium(University of Colorado at Boulder) to see evolutionary biology icon and one of my favorite scientists - Richard Dawkins - Monday night.
Dr. Dawkins was there to have a speech/lecture on very basic natural world questions - but very hard for ordinary people to answer - such as What are things made of? What is reality? Why do bad things happen? Are we alone?, etc.
His explanations are all evidence-based, precise and accurate. His book "The Magic of Reality" is even more descriptive and well explained, even middle school kids can understand.
It was just amazing to see him having a speech in auditorium and also in person at the book signing session.
He looked little older than I imagined, but maybe that's because I only have seen his old photos and movie clips.


Sunday, October 14, 2012

새로운 디자인

새로운 디자인으로 블로그를 바꿨다.
이미지 사이즈가 엄청 큰걸 요구하지만 그래도 사진 갤러리가 제일 맘에든다.
여전히 사진 작업해야 할것들이 조금 남아있고, 다른것들은.. 맘에 들지 않거나 제대로 작동하지 않는것들이 몇가지 있는데.. 그것들은 천천히 하나씩 고치기로 하고..
일단 오늘은 이걸로 끝.

Saturday, August 18, 2012

마젠토에 category attribute 생성 및 삭제

마젠토는 EAV(Entity Attribute Value) 모델을 사용함으로써 각각의 객체모델에 attribute과 attribute의 값어치(value)를 생성시킬수 있습니다.
예를들어 product model 같은경우 마젠토 admin에서 새로운 product attribute을 생성시킬수 있습니다. 그리고 그 attribute의 갯수는 얼마든지 무한대로 늘일수 있습니다.

하지만 category model의 경우 마젠토 admin에서 새로운 category attribute을 생성시키는 기능이 없기 때문에 새로운 attribute을 카테고리에 더하고 싶어도 할수가 없습니다.
사실 마젠토 admin에서 category attribute을 생성시키는 기능이 없는 것일뿐 못하는것은 아닙니다.
마젠토는 EAV 모델을 사용하고 있고 product model과 마찬가지로 category model 또한 entity이며 그 entity에는 attribute과 value를 생성시킬수 있기 때문에 category attribute도 생성시킬수 있습니다.

그렇다고 이것을 phpMyAdmin 이나 adminer 같은 데이터베이스 툴을 사용해서 데이터베이스에 직접 attribute을 생성시키는것은 많은 위험이 따르구요,
아래와 같은 코드로 손쉽게 생성 및 삭제시킬수 있습니다.

category attribute 생성
[ccln_php]
// 마젠토 로딩
Mage::app();

// EAV 모델 셋업 model
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

// 이미지 셀렉터 생성
$setup->addAttribute('catalog_category', 'extra_image', array(
'group' => 'General',
'input' => 'image',
'type' => 'varchar',
'label' => 'Slider Image',
'backend' => 'catalog/category_attribute_backend_image',
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));

// 텍스트 필드 생성
$setup->addAttribute('catalog_category', 'extra_text', array(
'group' => 'General',
'input' => 'textarea',
'type' => 'text',
'label' => 'SEO Text',
'backend' => '',
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));

// Yes, No같은 boolean value 생성
$setup->addAttribute('catalog_category', 'extra_boolean', array(
'group' => 'General',
'input' => 'select',
'type' => 'int',
'label' => 'Yes or No',
'backend' => '',
'source' => 'eav/entity_attribute_source_boolean',
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
?>
[/ccln_php]

위의 코드는 extra_image, extra_text 그리고 extra_boolean 이란 category attribute을 생성시키는 코드구요, 마젠토 admin에서 category로 들어가시면 각각의 카테고리에 3개의 새로생긴 필드를 찾을수 있습니다.
각각의 attribute에서
group은 attribute이 나오는 그룹
input은 input 필드의 타입
type은 데이터베이스에 저장되는 value의 type
label은 admin에 나오는 텍스트 레이블
backend는 input 필드가 사용할 model
source는 기본적으로 데이터베이스에 저장되어 있는 값어치
를 나타냅니다.

반대로 생성된 attribute을 삭제하는 방법은 아래 코드를 참조하세요.
[ccln_php]
// 마젠토 로딩
Mage::app();

// EAV 모델 셋업 model
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

// category attribute 삭제
$setup->removeAttribute('catalog_category', 'extra_image');
$setup->removeAttribute('catalog_category', 'extra_text');
$setup->removeAttribute('catalog_category', 'extra_boolean');
?>
[/ccln_php]

위의 코드는 새로 생성된 3개의 category attribute을 삭제시킵니다.

Wednesday, March 28, 2012

마젠토 콜렉션(Collection) 사용하기

마젠토 콜렉션(Collection)은 한번에 여러개의 객체(product, customer, category 등등)를 불러오는데 사용됩니다.
예를들어 카테고리 페이지에있는 모든 제품을 불러온다던지, 모든 커스토머(customer) 정보를 불러올때 유용하게 쓰입니다.

아래와 같은 php코드를 루트에 올려놓고 시험해 보세요.
[ccln_php]
require_once "app/Mage.php";
Mage::app();

// 모든 제품 불러오기
$collection = Mage::getResourceModel('catalog/product_collection'); // 첫번째 방법
$collection = Mage::getModel('catalog/product')->getCollection(); // 두번째 방법

// 모든 제품이 필요하지는 않으므로 5개만 로드합니다.
$collection->getSelect()->limit(5);
$collection->load(); // 콜렉션을 불러옵니다.
foreach($collection AS $product) {
// 제품의 모든 데이터 출력
print_r($product->getData());
}
[/ccln_php]
위의 코드를 실행해 보시면 제품의 모든 데이터가 출력이 됩니다.
콜렉션 객체를 불러올때 두가지 방법을 보여드렸는데 첫번째 방법과 두번째 방법의 결과가 약간은 틀린걸 확인할수 있습니다.

첫번째 방법은 최소한의 데이터만 로드함으로써 메모리의 사용을 최대한 줄이는 방법이고,
$collection = Mage::getResourceModel('catalog/product_collection');

두번째 방법은 제품의 모든 데이터를 불러옴으로써 메모리 사용에 상관없이 콜렉션을 사용하는 방법입니다.
$collection = Mage::getModel('catalog/product')->getCollection();

개인적으로 메모리 사용이 적은 첫번째 방법을 선호합니다.
그렇다고 필요한 제품의 데이터를 불러오지 못하는것은 아닙니다.
아래와 같이 addAttributeToSelect()을 사용해서 필요한 Attribute을 불러올수 있습니다.
[ccln_php]
require_once "app/Mage.php";
Mage::app();

$collection = Mage::getResourceModel('catalog/product_collection');

// 아래처럼 원하는 attribute을 콜렉션에 포함시킬수 있습니다.
$collection->addAttributeToSelect('price');
$collection->addAttributeToSelect('weight');

// 모든 제품이 필요하지는 않으므로 5개만 로드합니다.
$collection->getSelect()->limit(5);
$collection->load(); // 콜렉션을 불러옵니다.
foreach($collection AS $product) {
// 제품의 모든 데이터 출력
print_r($product->getData());
}
[/ccln_php]


Attribute으로 불러올 데이터를 addAttributeToFilter()를 사용해서 걸러낼수도 있습니다.
예를들어 id가 10보다 커야 한다던지 sku가 'abcd'를 포함해야 하는경우 아래와 같이 콜렉션을 설정할수 있습니다.
[ccln_php]
require_once "app/Mage.php";
Mage::app();

$collection = Mage::getResourceModel('catalog/product_collection');

// 아래처럼 원하는 attribute을 콜렉션에 포함시킬수 있습니다.
// product entity_id가 10보다 커야하는경우
$collection->addAttributeToFilter('entity_id', array('gt' => 10));

// price가 $100 이하여야 하는 경우
$collection->addAttributeToFilter('price', array('lt' => 100));

// sku가 'abcd'를 포함해야 하는 경우
$collection->addAttributeToFilter('sku', array('like' => '%abcd%'));

// sku가 'zzz1'이 아니어야 하는 경우
$collection->addAttributeToFilter('sku', array('neq' => 'zzz1'));

// description이 아무것도 없는 제품을 포함하는 경우
$collection->addAttributeToFilter('description', 'null');

$collection->load(); // 콜렉션을 불러옵니다.
foreach($collection AS $product) {
// 제품의 모든 데이터 출력
print_r($product->getData());
}
[/ccln_php]

이런식으로 addAttributeToSelect()나 addAttributeToFilter()를 사용해서 원하는 attribute을 콜렉션에 포함시키거나 필터를 적용해서 걸러낼수 있습니다.
콜렉션의 사용은 한번에 많은 객체를 불러와야 할때, 또 한번에 여러 객체의 정보를 수정해야할때 유용하게 쓰입니다.