마젠토 콜렉션(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을 콜렉션에 포함시키거나 필터를 적용해서 걸러낼수 있습니다.
콜렉션의 사용은 한번에 많은 객체를 불러와야 할때, 또 한번에 여러 객체의 정보를 수정해야할때 유용하게 쓰입니다.
Wednesday, March 28, 2012
Tuesday, March 20, 2012
네이버 미국,캐나다 지역 키워드광고 대행
네이버 미국,캐나다 지역 키워드광고 대행
네이버가 미주(미국,캐나다)지역으로 영역을 넓히면서(http://us.naver.com) 광고영업 또한 확장하고 있습니다.
네이버의 주된 광고상품은 크게 디스플레이 광고(배너 광고)와 키워드 광고가 있는데 이번에 제가 네이버 미주지역의 키워드 광고 대행을 맡았습니다.
미주지역에서의 네이버 광고는 크게 디스플레이광고와 키워드광고로 나뉠수 있는데요,
디스플레이광고는 주로 가격이 비싼 배너광고이고, 키워드광고는 키워드 검색결과내에 보여지는 좀더 저렴한(월$100부터) 광고입니다.
미주지역의 키워드 광고는 한국내 키워드광고 같은 PPC(Pay Per Click)나 CPC(Cost Per Click)이 아닌 CPM(Cost Per Milli)의 성격을 가집니다.
쉽게말해 한국에서의 키워드 광고는 사용자가 클릭하는 숫자 만큼 지불해야하지만, 미주지역의 키워드 광고는 사용자의 클릭수와 관계없이 정해진 금액으로 일정한 광고위치를 구매하는것입니다.
NHN USA Inc. 관계자에 따르면 아직 시작하는 단계이기 때문에 단순한 모델로 시작한다고 하는데요, 결국엔 미주지역의 키워드 광고도 한국의 키워드 광고처럼 PPC/CPC 모델로 바뀐다고 합니다.
미주지역 네이버 키워드 광고에 대한 자세한 내용 및 문의는 아래 링크를 참조해 주시기 바랍니다.
네이버 미주 키워드 광고 문의
Tuesday, March 6, 2012
Magento Certified Developer PLUS
Magento Certified Developer PLUS
I just passed the Magento Certified Developer Plus exam.
It's wasn't easy but I would say it wasn't too difficult either.
Probably that's because I've seen a lot of postings and comments on other websites and blogs about Certified Developer PLUS exam being too intense, and to be honest, I didn't feel like I was ready and I thought I would fail the exam even before I take it.
Overall, if you're familiar with the Magento fundamentals, have good enough experience on Magento and confident about custom module/extension development, you don't have to worry about the exam.
If you don't have much experience with Magento, but you studied the Magento Certification Study Guide thoroughly, you might be ok, but at the same time, you could feel that exam questions are tricky and confusing.
Other than that, don't take it until you feel comfortable with Magento.
P.S
I have 3+ years of experience with Magento from version 1.3 to 1.11, and have worked on more than 25+ Magento websites.
For more information about the certification: Magento Developer Certification
Thursday, January 19, 2012
준비하는 자가 승리한다
준비하는 자가 승리한다
불길이 무섭게 타올라도 끄는 방법이 있고
물결이 하늘을 뒤덮어도 막는 방법이 있으니
화는 위험한 때 있는 것이 아니고
편안할 때 있으며
복은 경사 때 있는 것이 아니라
근심할 때 있는 것이다.
- 매월당 김시습 -
불길이 무섭게 타올라도 끄는 방법이 있고
물결이 하늘을 뒤덮어도 막는 방법이 있으니
화는 위험한 때 있는 것이 아니고
편안할 때 있으며
복은 경사 때 있는 것이 아니라
근심할 때 있는 것이다.
- 매월당 김시습 -
Thursday, January 5, 2012
마젠토의 싱글턴 패턴
마젠토의 싱글턴 패턴
getSingleton()과 getModel()의 차이점과 사용법
싱글턴 패턴은 하나의 클래스에서 하나의 인스턴스만을 생성하도록 하는 소프트웨어 디자인 패턴중의 하나입니다.
마젠토에서 이런 싱글턴 패턴의 사용은 객체의 인스턴스 생성 남용을 막으려는 방법이고 결과적으로 메모리의 사용을 최소화하기 위한 방책입니다.
예를 들자면 데이터베이스 인스턴스가 가장 대표적인 예입니다.
페이지가 로딩되면서 여러번의 데이터베이스 사용이 필요할수 있지만 데이터베이스 연결 인스턴스는 하나면 족합니다.
데이터베이스를 사용할때마다 똑같은 데이터베이스에 연결시키는 인스턴스를 여러번 생성하게되면 불필요한 리소스를 낭비하게 됩니다.
이럴때 이미 생성된 데이터베이스 인스턴스를 재활용하고자 하는것이 기본적인 싱글턴 패턴의 컨셉트 입니다.
마젠토에서 객체 인스턴스를 생성하는 방법은 getSingleton('모델/클래스'); 또는 getModel('모델/클래스'); 이 사용되는데 getSingleton('모델/클래스')의 경우 인스턴스가 이미 생성되어있지 않은경우 getModel('모델/클래스') method를 호출하게 됩니다.
아래는 app/Mage.php의 getSingleton() 와 getMethod() method입니다.
[ccln_php]
// getSingleton
public static function getSingleton($modelClass='', array $arguments=array())
{
$registryKey = '_singleton/'.$modelClass;
if (!self::registry($registryKey)) {
self::register($registryKey, self::getModel($modelClass, $arguments));
}
return self::registry($registryKey);
}
// getModel
public static function getModel($modelClass = '', $arguments = array())
{
return self::getConfig()->getModelInstance($modelClass, $arguments);
}
[/ccln_php]
getModel()의 사용예
[ccln_php]
// $productId를 이용한 product 객체의 인스턴스 생성,
$product = Mage::getModel('catalog/product')->load($productId);
// product의 이름 가져오기
$name = $product->getName();
[/ccln_php]
getSingleton()의 사용예
[ccln_php]
// customer session 불러오기
$customerSession = Mage::getSingleton('customer/session');
// customer 인스턴스 불러오기
$customer = $customerSession->getCustomer();
[/ccln_php]
getModel()과 getSingleton()의 비교
[ccln_php]
// getSingleton()을 사용하여 product 객체를 product_id 34로 로딩
$product = Mage::getSingleton('catalog/product')->load(34);
echo $product->getId(); // 34 가 출력됨
// getModel()을 사용하여 product 객체를 로딩
$product = Mage::getModel('catalog/product');
echo $product->getId(); // 아무것도 출력되지 않음
// getModel()은 새로운 객체를 생성하기 때문에 아무것도 출력되지 않음
// getSingleton()을 사용하여 이미 존재하는 product 객체를 로딩
$product = Mage::getSingleton('catalog/product');
echo $product->getId(); // 34 가 출력됨
[/ccln_php]
line 11의 경우, getSingleton()은 이미 생성된 객체를 불러오기 때문에 굳이 product_id 를 로딩하지 않아도 위의 line 2에서 생성된 객체를 자동으로 로딩하게 됩니다.
그래서 line 12에서는 기존에 로딩되어있던 product 객체의 id를 출력하게 됩니다.
Subscribe to:
Posts (Atom)