A judge has ruled that a 24-year-old Canadian man is not allowed to have a girlfriend for the next three years.

The ruling came after Steven Cranley pleaded guilty on Tuesday to several charges stemming from an assault on a former girlfriend.

Cranley, who has been diagnosed with a dependent personality disorder, attacked his girlfriend in an argument after their breakup.

He tried to prevent her from phoning the police by cutting her phone cord and punched and kicked her. He finally stabbed himself with a butcher knife when police did arrive, puncturing his aorta.

Doctors say Cranley has difficulty coping with rejection and runs a high risk to re-offend if he becomes involved in another intimate relationship.

Justice Rhys Morgan said Cranley "cannot form a romantic relationship of an intimate nature with a female person.

"That is the only way I can see the protection of the public is in place until you get the counseling you need."

Cranley had already served 146 days in pre-trail custody, which Morgan said was enough jail time in this case.

His lawyer says the no girlfriend order is the first of its kind that he has encountered.