Larry Ullman's Blog, page 6

September 4, 2013

“Going Big” With a Website

I’ve created a three-part series posted as a guest blogger on the site of my�hosting company, ServInt. The series is titled “‘Going Big’ With a Website”, and is based upon a series of newsletters I put out a couple of months back. In the series, I attempt to explain how one should navigate the process of having no site, to having a site, to having an active site, should you be so fortunate. The series isn’t so much about the X’s and O’s (e.g., caching, using gzip, Apache vs. nginx, and so forth), as it is about general philosophies you need to consider in order to best use your limited resources (although there are a few specific recommendations in there).


The first post went live last week, and is subtitled “Infrastructure that Scales“. Part 2 is subtitled “Code that Scales“, and just went live on Tuesday, September 3rd. Part 3 looks at the database, and will be posted next week.


Also, for those of you wondering whom I use as my web host, it’s ServInt! I’ve been with them since maybe 2006 and have been extremely happy with their product and their excellent customer service.

 •  0 comments  •  flag
Share on Twitter
Published on September 04, 2013 08:42

August 26, 2013

“How to decide when to work for free” by Penelope Trunk

On August 17, 2013, at the Northeast PHP conference in Boston, I gave a new presentation titled “How To Get There“. This was a motivational-like presentation, which is virgin territory for me, but I think it turned out well. In the talk, I recommend 21 behaviors and attitudes one should have in order to succeed, in order to achieve one’s goals. The tl;dr version comes down to this:


Be honest. Work hard. Never stop learning.


This is common sense, yes, but often it’s the things that should go without saying that most need to be said.


One of the specific recommendations I have in the talk (one of my 21 points of light) is: Give It Away. This is actually one of my recommendations for making money. Ironically, one way of making money, one way of getting a great job, is to give your time or work away for free. This is absolutely, 100% valid, and I could relay dozens of stories about myself and others where giving it away has had remarkable financial rewards.


Coincidentally, I just today came across a blog post by Penelope Trunk titled “How to decide when to work for free“. In the article, Ms. Trunk wonderfully illuminates the benefits of working for free, of giving it away. Moreover, the article provides recommendations for how one makes the decisions as to when you should work for free and when you shouldn’t. If you’re looking to get a job, or just to build your resume, it’s well worth reading.

1 like ·   •  0 comments  •  flag
Share on Twitter
Published on August 26, 2013 08:18

August 20, 2013

My 3 Presentations from Northeast PHP 2013

Over the past weekend I was fortunate enough to give three presentations at the excellent Northeast PHP conference in Boston:



How To Get There
Ajax: You Can Do It, Too!
How to Teach PHP and Web Development

I think that the presentations went well, and I enjoyed the conference very much. If you were present for any of the presentations (thanks!), you can use the links above to go to the joind.in page to add your feedback (thanks!).


If you weren’t able to make the Northeast PHP conference, or were there but missed one of these presentations, you can view all the slides, with very detailed speaker notes (i.e., my scripts), at SpeakerDeck. Every presentation was video recorded, and I’ll post links to those once they exist.


My heartfelt thanks to everyone that made it a special point to come and say “hello” to me. Wonderful meeting you all!

 •  0 comments  •  flag
Share on Twitter
Published on August 20, 2013 05:31

August 16, 2013

“The Yii Book” Update and a Site Update

A couple of quick updates on “The Yii Book” and its corresponding website, yii.larryullman.com


First, yesterday I posted an update to “The Yii Book”. This includes two new chapters: Chapter 17, “Improving Performance,” and Chapter 18, “Advanced Database Issues”. This adds about 50+ more pages as a PDF, bringing the book’s total page count to around 460 total, with a few more chapters to go. You can download the latest version from the account page.


I labelled this version 0.75, despite the fact that the previous release was 0.71. It turns out that I haven’t spread out my version numbers properly to hit 1.0 at the right time. If I were doing better with that, this would be version 0.8. Or, if you want, we can call this version 0.8, but that would mean the book won’t be completed (in its initial draft) until version 1.2. Whatever you prefer.


The second piece of news may not make any difference to you, but I’ve finally integrated Mailgun for handling all the emails sent by my site. Hopefully, the change will be invisible to almost everyone, although a couple of you might be getting emails for the first time.


I adapted an external mail server for a couple of reasons.�First, the fact is that once your site gets to any decent level of activity, it’s no longer prudent to have your web server also act as your mail server. It just hurts performance too much.


Second, unless you really know what you’re doing, some percentage of emails won’t get through to the end user from your server (due to spam filters, configurations, and whatnot).�With “The Yii Book” as an example, there have been over 1,500 orders so far, meaning there have been over 1,500 receipt emails. Around 4 or 5 of those emails have bounced. Although this is an extra cost and required extra effort, certainly all customers deserve to receive an email receipt. (Even if the problem is with the configuration of the recipient’s server, which I know is the case in one situation.) Hopefully, the integration of Mailgun should prevent this from happening in the future. Or at least minimize the risk of bounces to the lowest possible level.


The third issue is that when I update the book, I send a mass email to let readers know (it goes to everyone that’s opted to receive updates; it doesn’t automatically go to every reader). This mass email slows down my server, will bounce for a few addresses, and runs the risk of getting my domain labelled as a spammer. So from now on, all email related to “The Yii Book”, coming from the site itself, will actually go through Mailgun (at a cost of $19/month, in case you’re interested).


(I had to update a bunch of code to make this work, but hopefully I haven’t introduced an serious bugs in doing so. I created my own skeleton class for using Mailgun via PHP. Maybe I’ll write about and share that later on.)


All that said,�I’m now off to Boston for the Northeast PHP conference. In fact, I’m posting this from the Philadelphia airport as I await my flight.


I’ll start writing the next chapters when I return.


As always, thanks to everyone for your interest in the book, for your feedback, and for your patience.

 •  0 comments  •  flag
Share on Twitter
Published on August 16, 2013 12:32

August 12, 2013

“The Yii Book” Status Update, #1,492.9

Just a quick update on the status of “The Yii Book” (the project that never ends)…


I’ve written several thousand words for�Chapter 17, “Improving Performance”. And I’ve finally completed a few thousand on for Chapter 18, “Advanced Database Issues”. I’m hoping to send those to the technical editors today or tomorrow at the latest, and then I’ll post the new chapters as soon as I get their feedback. Barring something unforeseen, this update should be out within a week to ten days at the latest.


As always, thanks to everyone for their patience and interest in the book!

 •  0 comments  •  flag
Share on Twitter
Published on August 12, 2013 06:27

August 9, 2013

Speaking at Conferences

As I’m preparing for my three presentations at Northeast PHP in Boston next weekend (August 16th-18th), I’ve been reviewing some of the resources I have regarding public speaking. I thought I’d share three great posts on speaking at conferences.



The first is from�Ashe Dryden and is titled�What You Need to Know About Speaking at Conferences. This is a long and detailed post, covering everything from getting started, coming up with good ideas, and writing quality abstracts, to actually creating the talk (although there’s not that much discussion on giving the talk).


The second is from the supremely talented�Kate Matsudaira and is titled�So you want to speak at a conference?. (I’ll forgive her the extraneous fourth period). The post has two broad subjects: getting a talk accepted and becoming a better public speaker. Both threads are full of great advice.


Finally, there’s�Davey Shafik‘s article�Speaking at Conferences: How to write a talk and get it accepted. His article is written from the perspective of a selection committee member for a conference. The focus in his article is naturally on how one gets a talk (or talks) accepted: what the organizers are looking for. If you’d like some insight into that side of things, this is the article for you.


If you’d like to do more public speaking yourself, read all three: you’ll be glad you did. And if you’re presenting at, or just attending, Northeast PHP next week, I’ll look forward to seeing you there!

 •  0 comments  •  flag
Share on Twitter
Published on August 09, 2013 11:49

August 7, 2013

Preview Videos for the Northeast PHP Conference

The good people behind the Northeast PHP conference (starting next Friday, August 16th!) have recently posted two videos of interviews with conference presenters. And one of them is me.



First, you can watch the clip with Jen Kramer, who is presenting at NEPHP for the second straight year. This year, Ms. Kramer will be doing a talk on responsive web design frameworks and an entire workshop specifically on Twitter Bootstrap 3.


There’s also a long video clip with yours truly. In it I blather on (as I’m prone to do) about:



Writing books
Being an English major in an IT field
Working with clients
My three presentations at Northeast PHP

Third, tonight at 7:30 (ET), there’s a live interview being done with Jonathan Klein. Mr. Klein works at Etsy, and will be giving�two talks at NEPHP: Practical Responsive Web Design and�Scaling PHP to 40 Million Uniques.


I’ve already identified what sessions I plan on attending at the conference, and talks by both Ms. Kramer and Mr. Klein are on my agenda. So watch the videos and if you haven’t bought your ticket to NEPHP, do so now!�I’ll look forward to seeing you there!

 •  0 comments  •  flag
Share on Twitter
Published on August 07, 2013 09:26

July 31, 2013

Join Me for a Live Online Interview Thursday Night

Tomorrow night, Thursday, August 1, at 7:30PM ET, I’ll be doing a live online interview with Matt Murphy of the Boston PHP group. In the interview, I’ll preview the three presentations I’ll be given at the Northeast PHP conference in Boston on August 17th and 18th. The plan is to broadcast the interview live (via Google Hangouts On Air, I think). If this all works, I’ll be able to answer questions sent to @nephp or @larryullman. For more information and details (to be determined), watch this page or @nephp, @bostonphp, and @larryullman.


Oh, and if you haven’t bought your ticket for Northeast PHP yet, what are you waiting for?

 •  0 comments  •  flag
Share on Twitter
Published on July 31, 2013 07:42

July 29, 2013

What is Larry Thinking? # 71 => On Joining Stripe

In this edition…



About This Newsletter
What Are You Thinking? => On Stripe, and Joining Stripe
On the Web => Stripe
Q&A => How does Stripe compare to X?
Q&A => How did you get the job?
Q&A => What will you be doing at Stripe?
What is Larry Thinking? => On Joining Stripe
Q&A => How will joining Stripe impact your other work?
Larry Ullman’s Book News => “The Yii Book” and “Effortless E-commerce with PHP and MySQL (2nd ed)”


About This Newsletter

It’s a beautiful, gorgeous, beautiful fall day here in central Pennsylvania (which is odd, because it’s late July), and it’s time for another newsletter. This is a special newsletter for me, as the topic surrounds a big change in my life. As of August 1, I’ll be working for Stripe. I officially accepted the position, and announced it, about a month ago, so you may have already known this, but I thought I’d spend a little time discussing Stripe, my personal decision, and how this might impact you. For the sake of breaking the newsletter into more readable chunks, I’ll present some content as if I’m answering a question I’ve been asked (although I actually haven’t been). Apologies for the artifice.


In another newsletter, I’ll write about my thoughts and experiences in working for myself, creating my own business, and “building up my brand”. Not that those parts of my working life are entirely switched off from here on out, but this is going to be an existential change, and there are aspects of having worked for myself for 14 years that went into the decision to join Stripe.


As always, questions, comments, and all feedback are much appreciated. And thanks for your interest in what I have to say and do!


What Are You Thinking? => On Stripe, and Joining Stripe

I’m curious if you have any first impressions on either Stripe as a company or product, or on my joining Stripe. Or, if you have any second impressions after reading this newsletter. Whatever the case, if you have some thoughts you’d like to share with me, please do!


On the Web => Stripe

In case you’re not familiar with the company, “Stripe makes it easy to start accepting�credit cards on the web today” (from Stripe’s web site). That’s really it, but it’s quite an accomplishment. As is always the case, it’s quite challenging to do something simple right.


If you’ve done e-commerce before, you know how hard it can be to integrate the payment processing. For example, to accept credit cards, you have to find a payment gateway and setup a merchant account. And if the card data is going through your server, you’ve got to do your due diligence to maintain PCI compliance. PCI compliance can be taxing, time consuming, and expensive.


You can drop (or simplify) the PCI compliance demand if you send your customers to another site for processing. But that’s not an ideal user experience, and introduces issues you’ll need to contend with (e.g., what if the customer doesn’t complete the payment?, what if they do but don’t return to your site?, etc.).


That’s if you want to accept credit cards. Don’t get me started on how difficult it is to integrate PayPal…


Stripe came up with a brilliantly simple solution to all this.�When you choose Stripe, you can be up and running in an hour (literally). All you need is an SSL certificate and a bank account. That’s it. No merchant accounts. No PCI compliance audits.�Stripe will automatically deposit your funds to your bank account, and without any extraneous fees. Stripe charges just 2.9% + $0.30 per successful charge.�Stripe has no setup fees, no monthly fees, no validation fees, no refund fees, and no card storage fees. There’s also no charge for failed payments.


Customers can use their credit cards to pay you, and they don’t need Stripe accounts or to register somewhere or anything like that. A valid credit card and you get paid.


Stripe is currently available in the US and Canada, and is undergoing a�private beta in the UK, Ireland, and France.�If you’re doing e-commerce, you really should check Stripe out. Stripe will make your life as a web developer so much easier. For more on using Stripe, you can see my blog series, which I wrote last year.


Q&A => How does Stripe compare to X?

The two most common competitors to Stripe (in terms of the approach that Stripe takes, not competitors as in other payment options such as PayPal) are:



Braintree
Paymill

So how does Stripe compare? Well, I can’t really say. I’ve not used these other services and I’m clearly biased. I’m sorry I can’t be more helpful in that regard, although searches online will turn up compare and contrast postings that others have done.


Q&A => How did you get the job?

In the winter and spring of 2012, I used Stripe on an e-commerce project for a client (at the client’s request; I had not previously been familiar with Stripe). In working through an issue, I ended up communicating with Patrick, one of Stripe’s cofounders, over Twitter. I was already planning on being out in San Francisco a short while later, and so we arranged to meet up. So in June of 2012, I met the Stripe cofounders at their single-story headquarters in the financial district of San Francisco. At the time, there were maybe 20 employees. I enjoyed meeting them, getting to see Stripe’s setup, and learning about their approach. I’ve not been part of a startup like this before, let alone one in San Francisco, so it was educational for me.


In the months after that meeting, I wrote my my blog series on using Stripe, during which time I had several conversations with Michael, from the Stripe support team. Then, in April of 2013, Michael sent out a tweet saying that Stripe was always looking for good people to join the support team, and to let him know if you knew of anyone. I, half-jokingly, replied “Me!”. Soon thereafter, I received a “Let’s talk.” email from Michael.


I didn’t really know what I was getting myself into and was worried that I had made a mistake in pretending to put myself forth as a candidate. You see: I’m thoroughly ensconced in central Pennsylvania (my wife’s, somewhat unique, job is here; my kids are in school; we’ve got a good life) and can’t move to San Francisco. Further, considering my other commitments?my family, my books, readers, and a publisher, I can’t take on a true full time job either. Apparently, these concerns are not problems for Stripe.


I had a few conversations with Michael, and sent some emails back and forth. The more I learned and saw, the more interested I became. Then I flew out to San Francisco, and spent a day actually doing the job and meeting the team. At that point (a couple months ago), Stripe was now in its own building, with about 50 employees.


I answered support questions that day, had a couple of informal interviews, and even did a live coding challenge while someone watched. That was intimidating! The day went well, and, again, I became more enamored with Stripe and what they’re doing (more on this later in the newsletter).


As I now know, Stripe does not hire casually, and so the next step was to do a three-week trial. That was three weeks of me doing the job. (Had you sent in a support question during that time, it was possible that you would have received a reply from larry@stripe.com!) After the trial, I was offered a position. Which I happily accepted.


Q&A => What will you be doing at Stripe?

My role at Stripe is as a “Support Engineer”, although Stripe isn’t too beholden to titles. From Stripe’s job description:


Stripe’s support engineers work with its users to answer questions and solve problems. They work with the rest of the team to ensure that those problems don’t arise again. Support engineers rapidly get to know both new people and new code. Interacting primarily through email and real-time chat, Stripe’s support engineers guide developers along the process of creating paid products. Stripe’s support team informs the direction of the Stripe product by painting a detailed picture of how Stripe is used.


In short, I’ll be helping people use Stripe, in both technical and non-technical ways. I could be answering questions about using the Stripe API with nearly any language, or I could address some simple confusion about how payments are transferred. Stripe takes support very seriously, and a ton of thought goes into doing it right. I’ll also work on developing internal Stripe tools.


As you can tell, the job fits in nicely with what I’ve been doing for the past decade.


What is Larry Thinking? => On Joining Stripe

I’ve been working for myself for 14 years now. Until recently, I’ve not considered going to work for someone else in any serious way. I make a good enough living as it is, and more importantly, I have supreme flexibility in terms of the hours and days I work. Those are the logistical benefits to working for myself. The intellectual benefit is that I can learn and do whatever I want. Truly, I’ve been quite spoiled and fortunate. And I’ve often worried that working for a company can end up pigeon-holing you into a role, thereby limiting the kids of things you can learn and do.


Good fortune aside, I’ve also hit somewhat of a wall in my career. Well, three walls. I’ll write about those more in a separate newsletter, but a key point is that my current work situation presents some limits as to what I can do and learn, largely due to the opportunities available to me. I’m thrilled with where I’m at in most ways, but I really want to challenge myself, to learn more and more, to work on something great, and to work with fantastic people. This is where Stripe comes in.


I have, I confess, dreamed about joining companies before: Apple, ESPN, etc. I never took any steps to pursue those possibilities, though. And the truth is that I’ve positioned myself in a way that makes me harder to employ. Because of my obligations to my family, to readers of my books, and to my publisher, I can’t realistically work 40+ hours anywhere. For Stripe, these logistics aren’t a problem. To Stripe, hours, days, and locations are immaterial if you’re someone they want to work with and you can do the job. Those seem to be the two most important criteria when it comes to hiring.


Moving over any logistical hurdles, obviously I’m a fan of what Stripe has accomplished. I think it’s really quite something, both valuable and impressive. When I spent a full day with the Stripe team in May, I came away even more impressed, for two primary reasons.


The first reason has to do with Woody Allen and his classic film Manhattan. For many of us movie buffs, Manhattan is a great, beautiful film (I agree, although some say it’s Allen’s best, whereas I prefer�Annie Hall). For Allen, he was always somewhat disappointed in Manhattan because he knew what he wanted to do, and the end result wasn’t quite that. To us, it’s a great movie. To Allen, it’s not quite what it should have been.


When I would meet people at Stripe, I would comment on how impressed I was with what they had accomplished and how proud I imagine they must be. To a person, the response was along the lines of “This is good, but what we really want is what Stripe is going to be.” That’s very enticing, and made me want to be a part of that team even more so. To me, from the outside, Stripe has created something great, that I’m not sure I could have ever envisioned, let alone implemented, and that’s just the start of it to them. My intellectual curiosity is salivating.


The second reason I am thrilled to join Stripe are the people employed there. I am in awe of the brilliance and accomplishments of these people. And not just the founders, but everyone in every position. Take a look at their profiles and you’ll see what I mean. I am in awe.


And they’re all very nice. And welcoming. Yeesh…


I like to think of myself as a pretty good writer and rather knowledgeable about technology, but I am truly humbled to be joining this group of people. I imagine it will be a while before I stop writing things like “Stripe employees an amazing array of talented, accomplished, and wonderful people (as well as myself)”.


I have no idea what the future will bring, but I am thrilled to be joining Stripe.


Q&A => How will joining Stripe impact your other work?

Because of my existing obligations?to my family, to readers of my books, and to my publisher?I have accepted the position in a part-time role (approximately 30 hours/week). But Stripe will be my primary work responsibility, and I want to honor my commitment to them. Towards that end, I am making some adjustments.


First, I will no longer be doing consulting for any clients save one (one of my very first clients, with whom I’ve worked for 12 years, a non-profit that only requires a couple of hours a month). I’ve already spoken with my existing clients to let them know, and to place them with new developers should they have that need.


Second, I’m not going to be writing as much. I still have “The Yii Book” to complete (for Yii 1 and Yii 2 and a print run). And I’ve just started writing the second edition of my “Effortless E-commerce with PHP and MySQL” book. From there on out, I won’t be writing as many books, particularly not as many new titles. Emphasis will be placed on updating and supporting my existing books, with the occasional article or two here or there. It’s not that I won’t be writing new books at all, but any possible new title will really have to be worth cutting into time with my family to make it happen.


I’ll also still continue to support my books through my forums.


In short:


If you bought a book of mine and need help, I’m still there for you.


I’m probably also going to cut back on my blog’s activity some, and won’t do more than one newsletter per month (which is a schedule I historically fall into, but never plan).


Third, I won’t be traveling as much (well, I’ll be traveling more to San Francisco where Stripe is headquartered, but not as much elsewhere). This means I won’t be doing any more on-site training, and I won’t be doing as many conferences. The latter has been a more recent (2012-2013) focus anyway. I’ll still be at Northeast PHP in August, of course.


I don’t think my joining Stripe is going to affect most people outside of my family (it’s truly an existential change for me and for them). Although one could argue that my joining Stripe will have a great benefit to you readers out there:�As I already mentioned, one of the reasons I’m joining Stripe is an opportunity to learn so much. I’ll be able to pass on much of that to you.


Larry Ullman’s Book News => “The Yii Book” and “Effortless E-commerce with PHP and MySQL (2nd ed)”

Last month (I think), I posted another update on “The Yii Book“. This is version 0.7, brining the total to 16 chapters and around 405 pages as a PDF.�I’ve completed the rough draft of Chapter 17, “Improving Performance,” and after I send out this newsletter, I begin Chapter 18, “Advanced Database Issues”. After that, I’ll post another update. Not sure when, exactly, that will be, but I’m hoping within the next two weeks. I definitely feel like I’m in the home stretch here.


The current plan is still to complete the first edition for the book using version 1 of the Yii framework. Then I’ll see where Yii 2 is at before making a decision about translations and print copies (i.e., �doing those for Yii 1 or waiting for Yii 2).


I’ve also begun writing the second edition of my “Effortless E-commerce with PHP and MySQL” book. I’ve already submitted the first two chapters, but they had only minor updates. The first edition was written in the fall of 2010, and has been very well received (out of 22 reviews at Amazon, 18 are 5-star). There was more I wanted to do in the first edition, and a few things have changed since the book was written, so my publisher and I felt it was time for a revision. The book has a release date in October or November.


For the second edition, here are the main changes and updates that I have planned:



A third payment processing example, using a system and approach that’s only been invented in the past couple years: Stripe!
Updated PayPal section to reflect changes in PayPal’s options and methodologies since the first edition.
A chapter on marketing-related functionality: supporting reviews, providing recommendations, and adding analytics.
A chapter on adding functionality using JavaScript and Ajax.
A chapter on implementing e-commerce functionality using OOP.

And, of course, all of the existing material is also being updated for clarity, fixing errors, and so forth.


Those are the key points. Right now, it looks like I’ll have 200 more pages for this edition (from approximately 350 pages to 550), so there should be a lot of room to expand ideas and introduce new ones.


If you have any thoughts on what you’d like to see in�the new edition, please let me know!

 •  0 comments  •  flag
Share on Twitter
Published on July 29, 2013 10:09

July 24, 2013

Using CDbCriteria in the Yii Framework

The CDbCriteria class let’s you customize queries through an object. The first thing you should do to become more comfortable with Active Record when using the Yii framework is master usage of CDbCriteria.


This is an excerpt from Chapter 8, “Working with Databases,” of “The Yii Book“.


To start, create a CDbCriteria instance:


$criteria = new CDbCriteria();

Then you can customize it by assigning values to various properties, the most important of which are listed in the following table.





Property
Sets




condition
The WHERE clause


limit
The LIMIT value


offset
The offset value in a LIMIT clause


order
The ORDER BY clause


params
The variables to be bound to the parameters


select
The columns to be selected



There are also properties for grouping and aggregating results, discussed elsewhere in the book.


As an easy example to begin, the same findByPk() query can be accomplished in this manner:


$criteria = new CDbCriteria();
$criteria->condition = 'id=:id';
$criteria->params = array(':id'=>$id);
$model = Page::model()->find($criteria);

To perform the same query using find() without formally creating a CDbCriteria object, just pass find() an array equivalent to what you would do with CDbCriteria:


$model = Page::model()->find(array(
'condition' => 'id=:id',
'params' => array(':id'=>$id)
));

{NOTE} When you’re finding a record using the primary key, it makes the most sense to use the findByPk() method. These other examples are for simple, comparative demonstrations.


As another example, this code might be used as part of the login process:


$criteria = new CDbCriteria();
$criteria->select = 'id, username';
$criteria->condition = 'email=:email AND pass=:pass';
$criteria->params = array(':email'=>$email, ':pass'=>$pass);
$model = User::model()->find($criteria);
Retrieving Multiple Records

The find() method only ever returns a single row (at most). If multiple rows should be returned by a query, use findAll() instead. Its signature is the same:


The class specification for the findAll() method.

The class specification for the findAll() method.


The findAll() method will return an array of objects, if one or more records match. If no records match, findAll() returns an empty array. This differs from find(), which returns NULL if no match was found.


Counting Records

Sometimes, you don’t actually need to return rows of data, but just determine how many rows apply to the given criteria. If you just want to see how many rows would be found by a query, use Active Record’s count() method. It takes the criteria as the first argument and parameters as the second, just like find():


// Find the number of "live" pages:
$criteria = new CDbCriteria();
$criteria->condition = 'live=1';
$count = Page::model()->count($criteria);

This is equivalent to running a SELECT COUNT(*) FROM page WHERE live=1 query and fetching the result into a number.


If you don’t care how many rows would be returned, but just want to confirm that at least one would be, use the exists() method:


$criteria = new CDbCriteria();
$criteria->condition = 'email=:email';
$criteria->params = array(':email'=>$email);
if (User::model()->exists($criteria)) {
$message = 'That email address has already been registered.';
} else {
$message = 'That email address is available.';
}
 •  0 comments  •  flag
Share on Twitter
Published on July 24, 2013 05:08

Larry Ullman's Blog

Larry Ullman
Larry Ullman isn't a Goodreads Author (yet), but they do have a blog, so here are some recent posts imported from their feed.
Follow Larry Ullman's blog with rss.